File Coverage

blib/lib/Crypt/RSA/Key/Public.pm
Criterion Covered Total %
statement 71 72 98.6
branch 20 26 76.9
condition 1 3 33.3
subroutine 15 15 100.0
pod 6 6 100.0
total 113 122 92.6


line stmt bran cond sub pod time code
1             package Crypt::RSA::Key::Public;
2 13     13   13462 use strict;
  13         103  
  13         366  
3 13     13   58 use warnings;
  13         63  
  13         369  
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 13     13   61 use vars qw($AUTOLOAD);
  13         25  
  13         442  
12 13     13   66 use Carp;
  13         24  
  13         633  
13 13     13   516 use Data::Dumper;
  13         6468  
  13         551  
14 13     13   69 use base 'Crypt::RSA::Errorhandler';
  13         24  
  13         1181  
15 13     13   893 use Math::BigInt try => 'GMP, Pari';
  13         19790  
  13         89  
16              
17             $Crypt::RSA::Key::Public::VERSION = '1.99';
18              
19             sub new {
20              
21 21     21 1 3543 my ($class, %params) = @_;
22 21         91 my $self = { Version => $Crypt::RSA::Key::Public::VERSION };
23 21 100       91 if ($params{Filename}) {
24 1         3 bless $self, $class;
25 1         5 $self = $self->read (%params);
26 1         4 return bless $self, $class;
27             } else {
28 20         183 return bless $self, $class;
29             }
30              
31             }
32              
33              
34             sub AUTOLOAD {
35 2083     2083   4809 my ($self, $value) = @_;
36 2083         3661 my $key = $AUTOLOAD; $key =~ s/.*:://;
  2083         6786  
37 2083 100       6695 if ($key =~ /^n|e$/) {
    50          
38 2058 100       5139 if (defined $value) {
39 42 100       147 if (ref $value eq 'Math::BigInt') {
    50          
40 31         86 $self->{$key} = $value;
41             } elsif (ref $value eq 'Math::Pari') {
42 0         0 $self->{$key} = Math::BigInt->new($value->pari2pv);
43             } else {
44 11         72 $self->{$key} = Math::BigInt->new("$value");
45             }
46             }
47 2058 100       6050 if (defined $self->{$key}) {
48 2057 100       5086 $self->{$key} = Math::BigInt->new("$self->{$key}") unless ref($self->{$key}) eq 'Math::BigInt';
49 2057         9147 return $self->{$key};
50             }
51 1         3 return;
52             } elsif ($key =~ /^Identity$/) {
53 25 100       91 $self->{$key} = $value if $value;
54 25         106 return $self->{$key};
55             }
56             }
57              
58              
59             sub DESTROY {
60              
61 28     28   477 my $self = shift;
62 28         630 undef $self;
63              
64             }
65              
66              
67             sub check {
68              
69 552     552 1 1330 my $self = shift;
70 552 50 33     3643 return $self->error ("Incomplete key.")
71             unless defined $self->n && defined $self->e;
72 552         2371 return 1;
73              
74             }
75              
76              
77             sub write {
78              
79 1     1 1 5 my ($self, %params) = @_;
80 1         8 $self->hide();
81 1         5 my $string = $self->serialize (%params);
82 1 50       232 open(my $disk, '>', $params{Filename}) or
83             croak "Can't open $params{Filename} for writing.";
84 1         13 binmode $disk;
85 1         11 print $disk $string;
86 1         49 close $disk;
87              
88             }
89              
90              
91             sub read {
92 1     1 1 3 my ($self, %params) = @_;
93 1 50       22 open(my $disk, '<', $params{Filename}) or
94             croak "Can't open $params{Filename} to read.";
95 1         4 binmode $disk;
96 1         14 my @key = <$disk>;
97 1         6 close $disk;
98 1         4 $self = $self->deserialize (String => \@key);
99 1         4 return $self;
100             }
101              
102              
103             sub serialize {
104              
105 2     2 1 13 my ($self, %params) = @_;
106             # Convert bigints to strings
107 2         5 foreach my $key (qw/n e/) {
108 4 50       139 $self->{$key} = $self->{$key}->bstr if ref($self->{$key}) eq 'Math::BigInt';
109             }
110 2         42 return Dumper $self;
111              
112             }
113              
114              
115             sub deserialize {
116              
117 2     2 1 68 my ($self, %params) = @_;
118 2         5 my $string = join'', @{$params{String}};
  2         9  
119 2         9 $string =~ s/\$VAR1 =//;
120 2         138 $self = eval $string;
121 2         11 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