| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Crypt::RSA::Key::Public; | 
| 2 | 5 |  |  | 5 |  | 19562 | use strict; | 
|  | 5 |  |  |  |  | 45 |  | 
|  | 5 |  |  |  |  | 123 |  | 
| 3 | 5 |  |  | 5 |  | 21 | use warnings; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 153 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | ## Crypt::RSA::Key::Public | 
| 6 |  |  |  |  |  |  | ## | 
| 7 |  |  |  |  |  |  | ## Copyright (c) 2001, Vipul Ved Prakash.  All rights reserved. | 
| 8 |  |  |  |  |  |  | ## This code is free software; you can redistribute it and/or modify | 
| 9 |  |  |  |  |  |  | ## it under the same terms as Perl itself. | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 5 |  |  | 5 |  | 24 | use vars qw($AUTOLOAD); | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 205 |  | 
| 12 | 5 |  |  | 5 |  | 20 | use Carp; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 294 |  | 
| 13 | 5 |  |  | 5 |  | 936 | use Data::Dumper; | 
|  | 5 |  |  |  |  | 9708 |  | 
|  | 5 |  |  |  |  | 216 |  | 
| 14 | 5 |  |  | 5 |  | 24 | use base 'Crypt::RSA::Errorhandler'; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 835 |  | 
| 15 | 5 |  |  | 5 |  | 1588 | use Math::BigInt try => 'GMP, Pari'; | 
|  | 5 |  |  |  |  | 21911 |  | 
|  | 5 |  |  |  |  | 56 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | $Crypt::RSA::Key::Public::VERSION = '1.99'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub new { | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 6 |  |  | 6 | 1 | 1014 | my ($class, %params) = @_; | 
| 22 | 6 |  |  |  |  | 23 | my $self    = { Version => $Crypt::RSA::Key::Public::VERSION }; | 
| 23 | 6 | 100 |  |  |  | 26 | if ($params{Filename}) { | 
| 24 | 1 |  |  |  |  | 3 | bless $self, $class; | 
| 25 | 1 |  |  |  |  | 7 | $self = $self->read (%params); | 
| 26 | 1 |  |  |  |  | 4 | return bless $self, $class; | 
| 27 |  |  |  |  |  |  | } else { | 
| 28 | 5 |  |  |  |  | 22 | return bless $self, $class; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 35 | 67 |  |  | 67 |  | 192 | my ($self, $value) = @_; | 
| 36 | 67 |  |  |  |  | 96 | my $key = $AUTOLOAD; $key =~ s/.*:://; | 
|  | 67 |  |  |  |  | 263 |  | 
| 37 | 67 | 100 |  |  |  | 246 | if ($key =~ /^n|e$/) { | 
|  |  | 50 |  |  |  |  |  | 
| 38 | 63 | 100 |  |  |  | 142 | if (defined $value) { | 
| 39 | 10 | 100 |  |  |  | 48 | if (ref $value eq 'Math::BigInt') { | 
|  |  | 50 |  |  |  |  |  | 
| 40 | 3 |  |  |  |  | 10 | $self->{$key} = $value; | 
| 41 |  |  |  |  |  |  | } elsif (ref $value eq 'Math::Pari') { | 
| 42 | 0 |  |  |  |  | 0 | $self->{$key} = Math::BigInt->new($value->pari2pv); | 
| 43 |  |  |  |  |  |  | } else { | 
| 44 | 7 |  |  |  |  | 50 | $self->{$key} = Math::BigInt->new("$value"); | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 63 | 100 |  |  |  | 781 | if (defined $self->{$key}) { | 
| 48 | 62 | 100 |  |  |  | 185 | $self->{$key} = Math::BigInt->new("$self->{$key}") unless ref($self->{$key}) eq 'Math::BigInt'; | 
| 49 | 62 |  |  |  |  | 374 | return $self->{$key}; | 
| 50 |  |  |  |  |  |  | } | 
| 51 | 1 |  |  |  |  | 2 | return; | 
| 52 |  |  |  |  |  |  | } elsif ($key =~ /^Identity$/) { | 
| 53 | 4 | 100 |  |  |  | 12 | $self->{$key} = $value if $value; | 
| 54 | 4 |  |  |  |  | 17 | return $self->{$key}; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub DESTROY { | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 7 |  |  | 7 |  | 474 | my $self = shift; | 
| 62 | 7 |  |  |  |  | 298 | undef $self; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub check { | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 10 |  |  | 10 | 1 | 21 | my $self = shift; | 
| 70 | 10 | 50 | 33 |  |  | 60 | return $self->error ("Incomplete key.") | 
| 71 |  |  |  |  |  |  | unless defined $self->n && defined $self->e; | 
| 72 | 10 |  |  |  |  | 47 | return 1; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub write { | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 1 |  |  | 1 | 1 | 5 | my ($self, %params) = @_; | 
| 80 | 1 |  |  |  |  | 9 | $self->hide(); | 
| 81 | 1 |  |  |  |  | 6 | my $string = $self->serialize (%params); | 
| 82 | 1 | 50 |  |  |  | 297 | open(my $disk, '>', $params{Filename}) or | 
| 83 |  |  |  |  |  |  | croak "Can't open $params{Filename} for writing."; | 
| 84 | 1 |  |  |  |  | 28 | binmode $disk; | 
| 85 | 1 |  |  |  |  | 17 | print $disk $string; | 
| 86 | 1 |  |  |  |  | 64 | close $disk; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub read { | 
| 92 | 1 |  |  | 1 | 1 | 3 | my ($self, %params) = @_; | 
| 93 | 1 | 50 |  |  |  | 25 | open(my $disk, '<', $params{Filename}) or | 
| 94 |  |  |  |  |  |  | croak "Can't open $params{Filename} to read."; | 
| 95 | 1 |  |  |  |  | 2 | binmode $disk; | 
| 96 | 1 |  |  |  |  | 18 | my @key = <$disk>; | 
| 97 | 1 |  |  |  |  | 6 | close $disk; | 
| 98 | 1 |  |  |  |  | 5 | $self = $self->deserialize (String => \@key); | 
| 99 | 1 |  |  |  |  | 4 | return $self; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub serialize { | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 1 |  |  | 1 | 1 | 3 | my ($self, %params) = @_; | 
| 106 |  |  |  |  |  |  | # Convert bigints to strings | 
| 107 | 1 |  |  |  |  | 3 | foreach my $key (qw/n e/) { | 
| 108 | 2 | 50 |  |  |  | 86 | $self->{$key} = $self->{$key}->bstr if ref($self->{$key}) eq 'Math::BigInt'; | 
| 109 |  |  |  |  |  |  | } | 
| 110 | 1 |  |  |  |  | 25 | return Dumper $self; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub deserialize { | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 1 |  |  | 1 | 1 | 3 | my ($self, %params) = @_; | 
| 118 | 1 |  |  |  |  | 3 | my $string = join'', @{$params{String}}; | 
|  | 1 |  |  |  |  | 5 |  | 
| 119 | 1 |  |  |  |  | 4 | $string =~ s/\$VAR1 =//; | 
| 120 | 1 |  |  |  |  | 75 | $self = eval $string; | 
| 121 | 1 |  |  |  |  | 4 | return $self; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | 1; | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 NAME | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Crypt::RSA::Key::Public -- RSA Public Key Management. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | $key = new Crypt::RSA::Key::Public; | 
| 135 |  |  |  |  |  |  | $key->write ( Filename => 'rsakeys/banquo.public' ); | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | $akey = new Crypt::RSA::Key::Public ( | 
| 138 |  |  |  |  |  |  | Filename => 'rsakeys/banquo.public' | 
| 139 |  |  |  |  |  |  | ); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | Crypt::RSA::Key::Public provides basic key management functionality for | 
| 145 |  |  |  |  |  |  | Crypt::RSA public keys. Following methods are available: | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =over 4 | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =item B | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | The constructor. Reads the public key from a disk file when | 
| 152 |  |  |  |  |  |  | called with a C argument. | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =item B | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | Causes the key to be written to a disk file specified by the | 
| 157 |  |  |  |  |  |  | C argument. | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =item B | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | Causes the key to be read from a disk file specified by | 
| 162 |  |  |  |  |  |  | C into the object. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =item B | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | Creates a Data::Dumper(3) serialization of the private key and | 
| 167 |  |  |  |  |  |  | returns the string representation. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =item B | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Accepts a serialized key under the C parameter and | 
| 172 |  |  |  |  |  |  | coverts it into the perl representation stored in the object. | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =item C | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | Check the consistency of the key. Returns undef on failure. | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =back | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head1 AUTHOR | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | Vipul Ved Prakash, Email@vipul.netE | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | Crypt::RSA::Key(3), Crypt::RSA::Key::Private(3) | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =cut | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  |  |