Alt-Crypt-RSA-BigInt
view release on metacpan or search on metacpan
lib/Crypt/RSA/Key/Public.pm view on Meta::CPAN
bless $self, $class;
$self = $self->read (%params);
return bless $self, $class;
} else {
return bless $self, $class;
}
}
sub AUTOLOAD {
my ($self, $value) = @_;
my $key = $AUTOLOAD; $key =~ s/.*:://;
if ($key =~ /^n|e$/) {
if (defined $value) {
if (ref $value eq 'Math::BigInt') {
$self->{$key} = $value;
} elsif (ref $value eq 'Math::Pari') {
$self->{$key} = Math::BigInt->new($value->pari2pv);
} else {
$self->{$key} = Math::BigInt->new("$value");
}
}
if (defined $self->{$key}) {
$self->{$key} = Math::BigInt->new("$self->{$key}") unless ref($self->{$key}) eq 'Math::BigInt';
return $self->{$key};
}
return;
} elsif ($key =~ /^Identity$/) {
$self->{$key} = $value if $value;
return $self->{$key};
}
}
sub DESTROY {
my $self = shift;
undef $self;
}
sub check {
my $self = shift;
return $self->error ("Incomplete key.")
unless defined $self->n && defined $self->e;
return 1;
}
sub write {
my ($self, %params) = @_;
$self->hide();
my $string = $self->serialize (%params);
open(my $disk, '>', $params{Filename}) or
croak "Can't open $params{Filename} for writing.";
binmode $disk;
print $disk $string;
close $disk;
}
sub read {
my ($self, %params) = @_;
open(my $disk, '<', $params{Filename}) or
croak "Can't open $params{Filename} to read.";
binmode $disk;
my @key = <$disk>;
close $disk;
$self = $self->deserialize (String => \@key);
return $self;
}
sub serialize {
my ($self, %params) = @_;
# Convert bigints to strings
foreach my $key (qw/n e/) {
$self->{$key} = $self->{$key}->bstr if ref($self->{$key}) eq 'Math::BigInt';
}
return Dumper $self;
}
sub deserialize {
my ($self, %params) = @_;
my $string = join'', @{$params{String}};
$string =~ s/\$VAR1 =//;
$self = eval $string;
return $self;
}
1;
=head1 NAME
Crypt::RSA::Key::Public -- RSA Public Key Management.
=head1 SYNOPSIS
$key = new Crypt::RSA::Key::Public;
$key->write ( Filename => 'rsakeys/banquo.public' );
$akey = new Crypt::RSA::Key::Public (
Filename => 'rsakeys/banquo.public'
);
=head1 DESCRIPTION
Crypt::RSA::Key::Public provides basic key management functionality for
Crypt::RSA public keys. Following methods are available:
=over 4
=item B<new()>
The constructor. Reads the public key from a disk file when
called with a C<Filename> argument.
=item B<write()>
( run in 1.849 second using v1.01-cache-2.11-cpan-524268b4103 )