File Coverage

blib/lib/GnuPG/Key.pm
Criterion Covered Total %
statement 41 47 87.2
branch 10 20 50.0
path n/a
condition 8 12 66.6
subroutine 5 7 71.4
pod 2 5 40.0
total 66 91 72.5


line stmt bran path cond sub pod time code
1               # Key.pm
2               # - providing an object-oriented approach to GnuPG keys
3               #
4               # Copyright (C) 2000 Frank J. Tobin <ftobin@cpan.org>
5               #
6               # This module is free software; you can redistribute it and/or modify it
7               # under the same terms as Perl itself.
8               #
9               # This program is distributed in the hope that it will be useful,
10               # but WITHOUT ANY WARRANTY; without even the implied warranty of
11               # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12               #
13               # $Id: Key.pm,v 1.10 2001/12/10 01:29:27 ftobin Exp $
14               #
15                
16               package GnuPG::Key;
17 7       7   4075 use Moo;
  7           31  
  7           46  
18 7       7   2431 use MooX::late;
  7           19  
  7           40  
19               with qw(GnuPG::HashInit);
20                
21               has [
22               qw( length
23               algo_num
24               hex_id
25               hex_data
26               creation_date
27               expiration_date
28               creation_date_string
29               expiration_date_string
30               fingerprint
31               usage_flags
32               )
33               ] => (
34               isa => 'Any',
35               is => 'rw',
36               );
37                
38               has [
39               qw(
40               signatures
41               revokers
42               revocations
43               pubkey_data
44               )] => (
45               isa => 'ArrayRef',
46               is => 'rw',
47               default => sub { [] },
48               );
49                
50               sub push_signatures {
51 2       2 0 148 my $self = shift;
52 2           4 push @{ $self->signatures }, @_;
  2           60  
53               }
54                
55               sub push_revocations {
56 0       0 0 0 my $self = shift;
57 0           0 push @{ $self->revocations }, @_;
  0           0  
58               }
59                
60               sub push_revokers {
61 5       5 0 139 my $self = shift;
62 5           16 push @{ $self->revokers }, @_;
  5           142  
63               }
64                
65               sub short_hex_id {
66 0       0 1 0 my ($self) = @_;
67 0           0 return substr $self->hex_id(), -8;
68               }
69                
70               sub compare {
71 7       7 1 129 my ($self, $other, $deep) = @_;
72                
73 7           47 my @string_comparisons = qw(
74               length
75               algo_num
76               hex_id
77               creation_date
78               creation_date_string
79               usage_flags
80               );
81                
82 7           17 my $field;
83 7           22 foreach $field (@string_comparisons) {
84 42 50         1109 return 0 unless $self->$field eq $other->$field;
85               }
86                
87 7           104 my @can_be_undef = qw(
88               hex_data
89               expiration_date
90               expiration_date_string
91               local_id
92               );
93 7           84 foreach $field (@can_be_undef) {
94 28 50   66     615 return 0 unless ((defined $self->$field && ( $self->$field ne '') ) == (defined $other->$field && ( $other->$field ne '')));
        66        
95 28 50   66     1359 if (defined $self->$field && ( $self->$field ne '') ) {
96 0 0         0 return 0 unless ($self->$field eq $other->$field);
97               }
98               }
99 7           96 my @objs = qw(
100               fingerprint
101               );
102 7           15 foreach $field (@objs) {
103 7 50         116 return 0 unless $self->$field->compare($other->$field, $deep);
104               }
105                
106 7 100   66     150 if (defined $deep && $deep) {
107 4           38 my @lists = qw(
108               signatures
109               revokers
110               revocations
111               );
112 4           11 my $i;
113 4           13 foreach my $list (@lists) {
114 12 50         60 return 0 unless @{$self->$list} == @{$other->$list};
  12           311  
  12           291  
115 12           94 for ( $i = 0; $i < scalar(@{$self->$list}); $i++ ) {
  14           416  
116 2 50         42 return 0
117               unless $self->$list->[$i]->compare($other->$list->[$i], $deep);
118               }
119               }
120                
121 4 50         36 return 0 unless @{$self->pubkey_data} == @{$other->pubkey_data};
  4           91  
  4           100  
122 4           51 for ( $i = 0; $i < scalar(@{$self->pubkey_data}); $i++ ) {
  11           702  
123 7 50         152 return 0 unless (0 == $self->pubkey_data->[$i]->bcmp($other->pubkey_data->[$i]));
124               }
125               }
126 7           146 return 1;
127               }
128                
129               1;
130                
131               __END__
132                
133               =head1 NAME
134                
135               GnuPG::Key - GnuPG Key Object
136                
137               =head1 SYNOPSIS
138                
139               # assumes a GnuPG::Interface object in $gnupg
140               my @keys = $gnupg->get_public_keys( 'ftobin' );
141                
142               # now GnuPG::PublicKey objects are in @keys
143                
144               =head1 DESCRIPTION
145                
146               GnuPG::Key objects are generally not instantiated on their
147               own, but rather used as a superclass of GnuPG::PublicKey,
148               GnuPG::SecretKey, or GnuPG::SubKey objects.
149                
150               =head1 OBJECT METHODS
151                
152               =head2 Initialization Methods
153                
154               =over 4
155                
156               =item new( I<%initialization_args> )
157                
158               This methods creates a new object. The optional arguments are
159               initialization of data members.
160                
161               =item hash_init( I<%args> ).
162                
163                
164               =item short_hex_id
165                
166               This returns the commonly-used short, 8 character short hex id
167               of the key.
168                
169               =item compare( I<$other>, I<$deep> )
170                
171               Returns non-zero only when this Key is identical to the other
172               GnuPG::Key. If $deep is present and non-zero, the key's associated
173               signatures, revocations, and revokers will also be compared.
174                
175               =back
176                
177               =head1 OBJECT DATA MEMBERS
178                
179               =over 4
180                
181               =item length
182                
183               Number of bits in the key.
184                
185               =item algo_num
186                
187               They algorithm number that the Key is used for.
188                
189               =item usage_flags
190                
191               The Key Usage flags associated with this key, represented as a string
192               of lower-case letters. Possible values include: (a) authenticate, (c)
193               certify, (e) encrypt, and (s) sign.
194                
195               A key may have any combination of them in any order. In addition to
196               these letters, the primary key has uppercase versions of the letters
197               to denote the _usable_ capabilities of the entire key, and a potential
198               letter 'D' to indicate a disabled key.
199                
200               See "key capabilities" DETAILS from the GnuPG sources for more
201               details.
202                
203               =item hex_data
204                
205               The data of the key. WARNING: this seems to have never been
206               instantiated, and should always be undef.
207                
208               =item pubkey_data
209                
210               A list of Math::BigInt objects that correspond to the public key
211               material for the given key. This member is empty on secret keys in
212               GnuPG 1.4. It is populated on secret keys In GnuPG >= 2.2.0.
213                
214               For DSA keys, the values are: prime (p), group order (q), group generator (g), y
215                
216               For RSA keys, the values are: modulus (n), exponent (e)
217                
218               For El Gamal keys, the values are: prime (p), group generator (g), y
219                
220               For more details, see: http://tools.ietf.org/html/rfc4880#page-42
221                
222               =item hex_id
223                
224               The long hex id of the key. This is not the fingerprint nor
225               the short hex id, which is 8 hex characters.
226                
227               =item creation_date_string
228                
229               =item expiration_date_string
230                
231               Formatted date of the key's creation and expiration. If the key has
232               no expiration, expiration_date_string will return undef.
233                
234               =item creation_date
235                
236               =item expiration_date
237                
238               Date of the key's creation and expiration, stored as the number of
239               seconds since midnight 1970-01-01 UTC. If the key has no expiration,
240               expiration_date will return undef.
241                
242               =item fingerprint
243                
244               A GnuPG::Fingerprint object.
245                
246               =item signatures
247                
248               A list of GnuPG::Signature objects embodying the signatures on this
249               key. For subkeys, the signatures are usually subkey-binding
250               signatures. For primary keys, the signatures are statements about the
251               key itself.
252                
253               =item revocations
254                
255               A list of revocations associated with this key, stored as
256               GnuPG::Signature objects (since revocations are a type of
257               certification as well). Note that a revocation of a primary key has a
258               different semantic meaning than a revocation associated with a subkey.
259                
260               =item revokers
261                
262               A list of GnuPG::Revoker objects associated with this key, indicating
263               other keys which are allowed to revoke certifications made by this
264               key.
265                
266               =back
267                
268               =head1 SEE ALSO
269                
270               L<GnuPG::Fingerprint>,
271               L<GnuPG::Signature>,
272               L<GnuPG::Revoker>,
273                
274               =cut