File Coverage

lib/Crypt/RSA/Key/Public.pm
Criterion Covered Total %
statement 75 75 100.0
branch 17 20 85.0
condition 6 11 54.5
subroutine 17 17 100.0
pod 6 6 100.0
total 121 129 93.8


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