<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package PGP::Packet::base_pubkey;
# this is a terrible name.  better names are invited.

use strict;
use vars qw(@ISA %pkmpicount %sigmpicount);
use PGP::Packet;

@ISA = qw(PGP::Packet);

use Carp;
use PGP::Unpackable;
use Digest::MD5;
use Digest::SHA1;

BEGIN {
    %pkmpicount = (1,  2,	# RSA
		   2,  2,	# RSA
		   3,  2,	# RSA
		   17, 4,	# DSA
		   16, 3);	# El Gamal

    %sigmpicount = (1,  1,	# RSA
		    2,  1,	# RSA
		    3,  1,	# RSA
		    17, 2);	# DSA

    no strict 'refs';

    foreach my $accessor (qw(ctag created expires pkalg modlength
			     fingerprint keyid)) {
	*{$accessor} = sub {
	    my ($this) = @_;
	    $this-&gt;{"parsed"} || $this-&gt;_parse();
	    $this-&gt;{$accessor};
	};
    }
};

sub _parse {
    my ($self) = @_;

    my $body = new PGP::Unpackable($self-&gt;{"bodyref"});

    my $version = $body-&gt;unpackC();
    $self-&gt;{"version"} = $version;
    if (($version == 2) || ($version == 3)) {
	# parse out stuff

	$self-&gt;{"created"} = $body-&gt;unpack("N");
	$self-&gt;{"expires"} = $body-&gt;unpack("n");
	if ($self-&gt;{"expires"} &gt; 0) {
	    $self-&gt;{"expires"} *= 86400;
	    $self-&gt;{"expires"} += $self-&gt;{"created"};
	}
	$self-&gt;{"pkalg"} = $body-&gt;unpackC();

	my $modulus = $body-&gt;unpack_mpi();
	my $exponent = $body-&gt;unpack_mpi();

	$self-&gt;{"modlength"} = $modulus-&gt;{"length"};

	# compute the fingerprint

	my $fingerprint = new Digest::MD5;
	$fingerprint-&gt;add($modulus-&gt;{"bits"});
	$fingerprint-&gt;add($exponent-&gt;{"bits"});
	$self-&gt;{"fingerprint"} = $fingerprint-&gt;digest;

	# "compute" the keyid

	$self-&gt;{"keyid"} = substr($modulus-&gt;{"bits"}, -8, 8);
    } elsif ($version == 4) {
	# parse out stuff

	$self-&gt;{"created"} = $body-&gt;unpack("N");
	$self-&gt;{"pkalg"} = $body-&gt;unpackC();

	my $nmpis = $pkmpicount{$self-&gt;{"pkalg"}};
	my @mpis;
	for my $i (1..$nmpis) {
	    push(@mpis, $body-&gt;unpack_mpi());
	}

	$self-&gt;{"modlength"} = $mpis[0]-&gt;{"length"};

	# compute the fingerprint
	my $fingerprint = new Digest::SHA1;
	$fingerprint-&gt;add("\x99");
	$fingerprint-&gt;add(pack("n",length($ {$self-&gt;{"bodyref"}})));
	$fingerprint-&gt;add($ {$self-&gt;{"bodyref"}});
	$self-&gt;{"fingerprint"} = $fingerprint-&gt;digest;

	# "compute" the keyid
	$self-&gt;{"keyid"} = substr($self-&gt;{"fingerprint"}, -8, 8);
    } else {
	confess("unknown public key version $version");
    }

    $self-&gt;{"parsed"} = 1;
}

sub hashdata {
    my ($self) = @_;
    
    ("\x99".
     pack("n", length($ {$self-&gt;bodyref}).
     $ {$self-&gt;bodyref}));
}

1;
</pre></body></html>