home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Public.pm < prev    next >
Encoding:
Perl POD Document  |  2003-01-08  |  3.2 KB  |  165 lines

  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. use lib qw(lib);
  13. use strict; 
  14. use vars qw($AUTOLOAD);
  15. use Crypt::RSA;
  16. use Carp;
  17. use Data::Dumper;
  18. use Crypt::RSA::Errorhandler;
  19. use Math::Pari qw(PARI pari2pv);
  20. use vars qw(@ISA);
  21. @ISA = qw(Crypt::RSA::Errorhandler);
  22.  
  23. sub new { 
  24.  
  25.     my ($class, %params) = @_; 
  26.     my $self    = { Version => $Crypt::RSA::Key::VERSION };
  27.     if ($params{Filename}) { 
  28.         bless $self, $class;
  29.         $self = $self->read (%params);
  30.         return bless $self, $class; 
  31.     } else { 
  32.         return bless $self, $class;
  33.     } 
  34.  
  35.  
  36.  
  37. sub AUTOLOAD { 
  38.     my ($self, $value) = @_;
  39.     my $key = $AUTOLOAD; $key =~ s/.*:://;
  40.     if ($key =~ /^n|e$/) { 
  41.         if (ref $value eq 'Math::Pari') { 
  42.             $self->{$key} = pari2pv($value)
  43.         } elsif ($value && !(ref $value)) { 
  44.             if ($value =~ /^0x/) { 
  45.                 $self->{$key} = pari2pv(Math::Pari::_hex_cvt($value));
  46.             } else { $self->{$key} = $value } 
  47.         }
  48.         my $return  = $self->{$key} || "";
  49.         $return = PARI("$return") if $return =~ /^\d+$/;
  50.         return $return;
  51.     } elsif ($key =~ /^Identity$/) { 
  52.         $self->{$key} = $value if $value;
  53.         return $self->{$key};
  54.     }
  55.         
  56.  
  57.  
  58. sub DESTROY { 
  59.  
  60.     my $self = shift; 
  61.     undef $self;
  62.  
  63. }
  64.  
  65.  
  66. sub check { 
  67.  
  68.     my $self = shift;
  69.     return $self->error ("Incomplete key.") unless $self->n && $self->e;
  70.     return 1;
  71.  
  72. }
  73.  
  74.  
  75. sub write { 
  76.  
  77.     my ($self, %params) = @_; 
  78.     $self->hide();
  79.     my $string = $self->serialize (%params); 
  80.     open DISK, ">$params{Filename}" || 
  81.         croak "Can't open $params{Filename} for writing.";
  82.     print DISK $string;
  83.     close DISK;
  84.  
  85.  
  86.  
  87. sub read { 
  88.     my ($self, %params) = @_;
  89.     open DISK, $params{Filename} or
  90.         croak "Can't open $params{Filename} to read.";
  91.     my @key = <DISK>; 
  92.     close DISK;
  93.     $self = $self->deserialize (String => \@key);
  94.     return $self;
  95. }
  96.  
  97.  
  98. sub serialize { 
  99.  
  100.     my ($self, %params) = @_;
  101.     return Dumper $self; 
  102.  
  103.  
  104.  
  105. sub deserialize { 
  106.  
  107.     my ($self, %params) = @_; 
  108.     my $string = join'', @{$params{String}}; 
  109.     $string =~ s/\$VAR1 =//;
  110.     $self = eval $string;
  111.     return $self;
  112.  
  113. }
  114.  
  115.     
  116. 1;
  117.  
  118. =head1 NAME
  119.  
  120. Crypt::RSA::Key::Public -- RSA Public Key Management.
  121.  
  122. =head1 SYNOPSIS
  123.  
  124.     $key = new Crypt::RSA::Key::Public; 
  125.     $key->write ( Filename => 'rsakeys/banquo.public' );
  126.  
  127.     $akey = new Crypt::RSA::Key::Public (
  128.                 Filename => 'rsakeys/banquo.public' 
  129.             );
  130.  
  131.  
  132. =head1 DESCRIPTION
  133.  
  134. Crypt::RSA::Key::Public provides basic key management functionality for
  135. Crypt::RSA public keys. Following methods are available:
  136.  
  137. =over 4
  138.  
  139. =item B<new()>
  140.  
  141. The constructor. Reads the public key from a disk file when called with a
  142. C<Filename> argument.
  143.  
  144. =item B<write()>
  145.  
  146. Writes a public key to disk when called with a C<Filename> argument.
  147.  
  148. =over
  149.  
  150. =head1 AUTHOR
  151.  
  152. Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
  153.  
  154. =head1 SEE ALSO
  155.  
  156. Crypt::RSA::Key(3), Crypt::RSA::Key::Private(3)
  157.  
  158. =cut
  159.  
  160.  
  161.