| 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 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use base 'Crypt::RSA::Errorhandler'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use Math::Pari qw(PARI pari2pv); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 10 | $Crypt::RSA::Key::Public::VERSION = '1.99'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 18 | my ($class, %params) = @_; | |||||||||||||||||||||||||||
| 24 | 18 | my $self = { Version => $Crypt::RSA::Key::Public::VERSION }; | |||||||||||||||||||||||||||
| 25 | 18 | ▶ | if ($params{Filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 27 | 1 | $self = $self->read (%params); | |||||||||||||||||||||||||||
| 28 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||
| 30 | 17 | return bless $self, $class; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4518 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 37 | 4518 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 38 | 4518 | my $key = $AUTOLOAD; $key =~ s/.*:://; | |||||||||||||||||||||||||||
| 39 | 4518 | ▶ | if ($key =~ /^n|e$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 4498 | ▶ | if (ref $value eq 'Math::Pari') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 14 | $self->{$key} = pari2pv($value) | |||||||||||||||||||||||||||
| 42 | ▶ | } elsif ($value && !(ref $value)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$value and not ref $value
MC/DC50%1/2
$value and not ref $value
$value
not ref $value
Truth table66%2/3
A and not(B)
A= $valueB= not ref $value
| |||||||||||||||||||||||||||||
| 43 | 20 | ▶ | if ($value =~ /^0x/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | $self->{$key} = pari2pv(Math::Pari::_hex_cvt($value)); | |||||||||||||||||||||||||||
| 45 | 19 | } else { $self->{$key} = $value } | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 4498 | ▶ | my $return = $self->{$key} || ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$key} || ''
MC/DC100%1/1
$self->{$key} || ''
$self->{$key}
Truth table100%2/2
A
A= $self->{$key}
| |||||||||||||||||||||||||||||
| 48 | 4498 | ▶ | $return = PARI("$return") if $return =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 4498 | return $return; | |||||||||||||||||||||||||||
| 50 | } elsif ($key =~ /^Identity$/) { | ||||||||||||||||||||||||||||
| 51 | 20 | ▶ | $self->{$key} = $value if $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 20 | return $self->{$key}; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 219 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 219 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 219 | undef $self; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1092 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1092 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 1092 | ▶ | return $self->error ("Incomplete key.") unless $self->n && $self->e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->n and $self->e
MC/DC0%0/2
$self->n and $self->e
$self->n
$self->e
Truth table33%1/3
A and B
A= $self->nB= $self->e
| |||||||||||||||||||||||||||||
| 70 | 1092 | return 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 78 | 1 | $self->hide(); | |||||||||||||||||||||||||||
| 79 | 1 | my $string = $self->serialize (%params); | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | open DISK, ">$params{Filename}" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | croak "Can't open $params{Filename} for writing."; | ||||||||||||||||||||||||||||
| 82 | 1 | binmode DISK; | |||||||||||||||||||||||||||
| 83 | 1 | print DISK $string; | |||||||||||||||||||||||||||
| 84 | 1 | close DISK; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 1 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 91 | open DISK, $params{Filename} or | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | croak "Can't open $params{Filename} to read."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | binmode DISK; | |||||||||||||||||||||||||||
| 94 | 1 | my @key = <DISK>; | |||||||||||||||||||||||||||
| 95 | 1 | close DISK; | |||||||||||||||||||||||||||
| 96 | 1 | $self = $self->deserialize (String => \@key); | |||||||||||||||||||||||||||
| 97 | 1 | return $self; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 104 | 1 | return Dumper $self; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | sub deserialize { | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 112 | 1 | my $string = join'', @{$params{String}}; | |||||||||||||||||||||||||||
| 113 | 1 | $string =~ s/\$VAR1 =//; | |||||||||||||||||||||||||||
| 114 | 1 | $self = eval $string; | |||||||||||||||||||||||||||
| 115 | 1 | return $self; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 10 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Crypt::RSA::Key::Public -- RSA Public Key Management. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | $key = new Crypt::RSA::Key::Public; | ||||||||||||||||||||||||||||
| 129 | $key->write ( Filename => 'rsakeys/banquo.public' ); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $akey = new Crypt::RSA::Key::Public ( | ||||||||||||||||||||||||||||
| 132 | Filename => 'rsakeys/banquo.public' | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Crypt::RSA::Key::Public provides basic key management functionality for | ||||||||||||||||||||||||||||
| 139 | Crypt::RSA public keys. Following methods are available: | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =over 4 | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item B<new()> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | The constructor. Reads the public key from a disk file when | ||||||||||||||||||||||||||||
| 146 | called with a C<Filename> argument. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item B<write()> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Causes the key to be written to a disk file specified by the | ||||||||||||||||||||||||||||
| 151 | C<Filename> argument. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item B<read()> | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Causes the key to be read from a disk file specified by | ||||||||||||||||||||||||||||
| 156 | C<Filename> into the object. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item B<serialize()> | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Creates a Data::Dumper(3) serialization of the private key and | ||||||||||||||||||||||||||||
| 161 | returns the string representation. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item B<deserialize()> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Accepts a serialized key under the C<String> parameter and | ||||||||||||||||||||||||||||
| 166 | coverts it into the perl representation stored in the object. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item C<check()> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Check the consistency of the key. Returns undef on failure. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =back | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Vipul Ved Prakash, E<lt>mail@vipul.netE<gt> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Crypt::RSA::Key(3), Crypt::RSA::Key::Private(3) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||