File Coverage

blib/lib/Crypt/OpenPGP/KeyBlock.pm
Criterion Covered Total %
statement 40 45 88.8
branch 8 14 57.1
condition 2 6 33.3
subroutine 11 14 78.5
pod 6 12 50.0
total 67 91 73.6


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::KeyBlock;
2 8     8   42 use strict;
  8         15  
  8         340  
3              
4 8     8   3910 use Crypt::OpenPGP::PacketFactory;
  8         21  
  8         5258  
5              
6             sub primary_uid {
7             $_[0]->{pkt}{ 'Crypt::OpenPGP::UserID' } ?
8 43 50   43 0 11650174 $_[0]->{pkt}{ 'Crypt::OpenPGP::UserID' }->[0]->id : undef;
9             }
10              
11 23     23 0 10958 sub key { $_[0]->get('Crypt::OpenPGP::Certificate')->[0] }
12 0     0 0 0 sub subkey { $_[0]->get('Crypt::OpenPGP::Certificate')->[1] }
13              
14             sub encrypting_key {
15 27     27 1 67 my $kb = shift;
16 27         105 my $keys = $kb->get('Crypt::OpenPGP::Certificate');
17 27 50 33     179 return unless $keys && @$keys;
18 27         65 for my $key (@$keys) {
19 51 100       172 return $key if $key->can_encrypt;
20             }
21             }
22              
23             sub signing_key {
24 11     11 1 27 my $kb = shift;
25 11         46 my $keys = $kb->get('Crypt::OpenPGP::Certificate');
26 11 50 33     94 return unless $keys && @$keys;
27 11         34 for my $key (@$keys) {
28 11 50       74 return $key if $key->can_sign;
29             }
30             }
31              
32             sub key_by_id { $_[0]->{keys_by_id}->{$_[1]} ||
33 0 0   0 0 0 $_[0]->{keys_by_short_id}->{$_[1]} }
34              
35             sub new {
36 22     22 1 52 my $class = shift;
37 22         60 my $kb = bless { }, $class;
38 22         102 $kb->init(@_);
39             }
40              
41             sub init {
42 22     22 0 39 my $kb = shift;
43 22         80 $kb->{pkt} = { };
44 22         73 $kb->{order} = [ ];
45 22         59 $kb->{keys_by_id} = { };
46 22         81 $kb;
47             }
48              
49             sub add {
50 100     100 1 150 my $kb = shift;
51 100         161 my($pkt) = @_;
52 100         130 push @{ $kb->{pkt}->{ ref($pkt) } }, $pkt;
  100         362  
53 100         138 push @{ $kb->{order} }, $pkt;
  100         209  
54 100 100       392 if (ref($pkt) eq 'Crypt::OpenPGP::Certificate') {
55 39         156 my $kid = $pkt->key_id;
56 39         132 $kb->{keys_by_id}{ $kid } = $pkt;
57 39         225 $kb->{keys_by_short_id}{ substr $kid, -4, 4 } = $pkt;
58             }
59             }
60              
61 65     65 0 3920 sub get { $_[0]->{pkt}->{ $_[1] } }
62              
63             sub save {
64 2     2 1 5 my $kb = shift;
65 2         4 Crypt::OpenPGP::PacketFactory->save( @{ $kb->{order} } );
  2         35  
66             }
67              
68             sub save_armoured {
69 0     0 1   my $kb = shift;
70 0           require Crypt::OpenPGP::Armour;
71 0           Crypt::OpenPGP::Armour->armour(
72             Data => $kb->save,
73             Object => 'PUBLIC KEY BLOCK'
74             );
75             }
76              
77             1;
78             __END__
79              
80             =head1 NAME
81              
82             Crypt::OpenPGP::KeyBlock - Key block object
83              
84             =head1 SYNOPSIS
85              
86             use Crypt::OpenPGP::KeyBlock;
87              
88             my $packet = Crypt::OpenPGP::UserID->new( Identity => 'foo' );
89             my $kb = Crypt::OpenPGP::KeyBlock->new;
90             $kb->add($packet);
91              
92             my $serialized = $kb->save;
93              
94             =head1 DESCRIPTION
95              
96             I<Crypt::OpenPGP::KeyBlock> represents a single keyblock in a keyring.
97             A key block is essentially just a set of associated keys containing
98             exactly one master key, zero or more subkeys, some user ID packets, some
99             signatures, etc. The key is that there is only one master key
100             associated with each keyblock.
101              
102             =head1 USAGE
103              
104             =head2 Crypt::OpenPGP::KeyBlock->new
105              
106             Constructs a new key block object and returns that object.
107              
108             =head2 $kb->encrypting_key
109              
110             Returns the key that performs encryption in this key block. For example,
111             if a DSA key is the master key in a key block with an ElGamal subkey,
112             I<encrypting_key> returns the ElGamal subkey certificate, because DSA
113             keys do not perform encryption/decryption.
114              
115             =head2 $kb->signing_key
116              
117             Returns the key that performs signing in this key block. For example,
118             if a DSA key is the master key in a key block with an ElGamal subkey,
119             I<encrypting_key> returns the DSA master key certificate, because DSA
120             supports signing/verification.
121              
122             =head2 $kb->add($packet)
123              
124             Adds the packet I<$packet> to the key block. If the packet is a PGP
125             certificate (a I<Crypt::OpenPGP::Certificate> object), the certificate
126             is also added to the internal key-management mechanism.
127              
128             =head2 $kb->save
129              
130             Serializes each of the packets contained in the I<KeyBlock> object,
131             in order, and returns the serialized data. This output can then be
132             fed to I<Crypt::OpenPGP::Armour> for ASCII-armouring, for example,
133             or can be written out to a keyring file.
134              
135             =head2 $kb->save_armoured
136              
137             Saves an armoured version of the keyblock (this is useful for exporting
138             public keys).
139              
140             =head1 AUTHOR & COPYRIGHTS
141              
142             Please see the Crypt::OpenPGP manpage for author, copyright, and
143             license information.
144              
145             =cut