diff options
author | Norbert Preining <preining@logic.at> | 2010-02-28 09:36:33 +0000 |
---|---|---|
committer | Norbert Preining <preining@logic.at> | 2010-02-28 09:36:33 +0000 |
commit | 5589232e99f0fa13868111280c5b64ce9dc25730 (patch) | |
tree | 28f2e6ff422108ac87934aff9c3fc5911963625c /Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm | |
parent | a6f5de6eed80d66fbd23ceabda7edf22b8162236 (diff) |
add a tlperl.straw directory for public inspection
git-svn-id: svn://tug.org/texlive/trunk@17235 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm')
-rwxr-xr-x | Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm b/Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm new file mode 100755 index 00000000000..5784bbb11ac --- /dev/null +++ b/Master/tlpkg/tlperl.straw/lib/Crypt/OpenPGP/KeyBlock.pm @@ -0,0 +1,145 @@ +package Crypt::OpenPGP::KeyBlock; +use strict; + +use Crypt::OpenPGP::PacketFactory; + +sub primary_uid { + $_[0]->{pkt}{ 'Crypt::OpenPGP::UserID' } ? + $_[0]->{pkt}{ 'Crypt::OpenPGP::UserID' }->[0]->id : undef; +} + +sub key { $_[0]->get('Crypt::OpenPGP::Certificate')->[0] } +sub subkey { $_[0]->get('Crypt::OpenPGP::Certificate')->[1] } + +sub encrypting_key { + my $kb = shift; + my $keys = $kb->get('Crypt::OpenPGP::Certificate'); + return unless $keys && @$keys; + for my $key (@$keys) { + return $key if $key->can_encrypt; + } +} + +sub signing_key { + my $kb = shift; + my $keys = $kb->get('Crypt::OpenPGP::Certificate'); + return unless $keys && @$keys; + for my $key (@$keys) { + return $key if $key->can_sign; + } +} + +sub key_by_id { $_[0]->{keys_by_id}->{$_[1]} || + $_[0]->{keys_by_short_id}->{$_[1]} } + +sub new { + my $class = shift; + my $kb = bless { }, $class; + $kb->init(@_); +} + +sub init { + my $kb = shift; + $kb->{pkt} = { }; + $kb->{order} = [ ]; + $kb->{keys_by_id} = { }; + $kb; +} + +sub add { + my $kb = shift; + my($pkt) = @_; + push @{ $kb->{pkt}->{ ref($pkt) } }, $pkt; + push @{ $kb->{order} }, $pkt; + if (ref($pkt) eq 'Crypt::OpenPGP::Certificate') { + my $kid = $pkt->key_id; + $kb->{keys_by_id}{ $kid } = $pkt; + $kb->{keys_by_short_id}{ substr $kid, -4, 4 } = $pkt; + } +} + +sub get { $_[0]->{pkt}->{ $_[1] } } + +sub save { + my $kb = shift; + Crypt::OpenPGP::PacketFactory->save( @{ $kb->{order} } ); +} + +sub save_armoured { + my $kb = shift; + require Crypt::OpenPGP::Armour; + Crypt::OpenPGP::Armour->armour( + Data => $kb->save, + Object => 'PUBLIC KEY BLOCK' + ); +} + +1; +__END__ + +=head1 NAME + +Crypt::OpenPGP::KeyBlock - Key block object + +=head1 SYNOPSIS + + use Crypt::OpenPGP::KeyBlock; + + my $packet = Crypt::OpenPGP::UserID->new( Identity => 'foo' ); + my $kb = Crypt::OpenPGP::KeyBlock->new; + $kb->add($packet); + + my $serialized = $kb->save; + +=head1 DESCRIPTION + +I<Crypt::OpenPGP::KeyBlock> represents a single keyblock in a keyring. +A key block is essentially just a set of associated keys containing +exactly one master key, zero or more subkeys, some user ID packets, some +signatures, etc. The key is that there is only one master key +associated with each keyblock. + +=head1 USAGE + +=head2 Crypt::OpenPGP::KeyBlock->new + +Constructs a new key block object and returns that object. + +=head2 $kb->encrypting_key + +Returns the key that performs encryption in this key block. For example, +if a DSA key is the master key in a key block with an ElGamal subkey, +I<encrypting_key> returns the ElGamal subkey certificate, because DSA +keys do not perform encryption/decryption. + +=head2 $kb->signing_key + +Returns the key that performs signing in this key block. For example, +if a DSA key is the master key in a key block with an ElGamal subkey, +I<encrypting_key> returns the DSA master key certificate, because DSA +supports signing/verification. + +=head2 $kb->add($packet) + +Adds the packet I<$packet> to the key block. If the packet is a PGP +certificate (a I<Crypt::OpenPGP::Certificate> object), the certificate +is also added to the internal key-management mechanism. + +=head2 $kb->save + +Serializes each of the packets contained in the I<KeyBlock> object, +in order, and returns the serialized data. This output can then be +fed to I<Crypt::OpenPGP::Armour> for ASCII-armouring, for example, +or can be written out to a keyring file. + +=head2 $kb->save_armoured + +Saves an armoured version of the keyblock (this is useful for exporting +public keys). + +=head1 AUTHOR & COPYRIGHTS + +Please see the Crypt::OpenPGP manpage for author, copyright, and +license information. + +=cut |