home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / MD5.pm < prev    next >
Text File  |  1996-08-12  |  7KB  |  264 lines

  1. # SCCS ID @(#)MD5.pm    1.9 96/06/28
  2. package MD5;
  3.  
  4. use strict;
  5. use vars qw($VERSION @ISA @EXPORT);
  6.  
  7. require Exporter;
  8. require DynaLoader;
  9. require AutoLoader;
  10.  
  11. @ISA = qw(Exporter AutoLoader DynaLoader);
  12. # Items to export into callers namespace by default. Note: do not export
  13. # names by default without a very good reason. Use EXPORT_OK instead.
  14. # Do not simply export all your public functions/methods/constants.
  15. @EXPORT = qw(
  16.     
  17. );
  18. $VERSION = '1.7';
  19.  
  20. bootstrap MD5 $VERSION;
  21.  
  22. # Preloaded methods go here.
  23.  
  24. sub addfile
  25. {
  26.     no strict 'refs';    # Countermand any strct refs in force so that we
  27.             # can still handle file-handle names.
  28.  
  29.     my ($self, $handle) = @_;
  30.     my ($package, $file, $line) = caller;
  31.     my ($data) = '';
  32.  
  33.     if (!ref($handle))
  34.     {
  35.     # Old-style passing of filehandle by name. We need to add
  36.     # the calling package scope qualifier, if there is not one
  37.     # supplied already.
  38.  
  39.     $handle = $package . '::' . $handle unless ($handle =~ /(\:\:|\')/);
  40.     }
  41.  
  42.     while (read($handle, $data, 1024))
  43.     {
  44.     $self->add($data);
  45.     }
  46. }
  47.  
  48. sub hexdigest
  49. {
  50.     my ($self) = shift;
  51.  
  52.     unpack("H*", ($self->digest()));
  53. }
  54.  
  55. sub hash
  56. {
  57.     my ($self, $data) = @_;
  58.  
  59.     if (ref($self))
  60.     {
  61.     # This is an instance method call so reset the current context
  62.  
  63.     $self->reset();
  64.     }
  65.     else
  66.     {
  67.     # This is a static method invocation, create a temporary MD5 context
  68.  
  69.     $self = new MD5;
  70.     }
  71.  
  72.     # Now do the hash
  73.  
  74.     $self->add($data);
  75.     $self->digest();
  76. }
  77.  
  78. sub hexhash
  79. {
  80.     my ($self, $data) = @_;
  81.  
  82.     unpack("H*", ($self->hash($data)));
  83. }
  84.  
  85. # Autoload methods go after =cut, and are processed by the autosplit program.
  86.  
  87. 1;
  88. __END__
  89.  
  90. =head1 NAME
  91.  
  92. MD5 - Perl interface to the RSA Data Security Inc. MD5 Message-Digest Algorithm
  93.  
  94. =head1 SYNOPSIS
  95.  
  96.     use MD5;
  97.     
  98.     $context = new MD5;
  99.     $context->reset();
  100.     
  101.     $context->add(LIST);
  102.     $context->addfile(HANDLE);
  103.     
  104.     $digest = $context->digest();
  105.     $string = $context->hexdigest();
  106.  
  107.     $digest = MD5->hash(SCALAR);
  108.     $string = MD5->hexhash(SCALAR);
  109.  
  110. =head1 DESCRIPTION
  111.  
  112. The B<MD5> module allows you to use the RSA Data Security Inc. MD5
  113. Message Digest algorithm from within Perl programs.
  114.  
  115. A new MD5 context object is created with the B<new> operation.
  116. Multiple simultaneous digest contexts can be maintained, if desired.
  117. The context is updated with the B<add> operation which adds the
  118. strings contained in the I<LIST> parameter. Note, however, that
  119. C<add('foo', 'bar')>, C<add('foo')> followed by C<add('bar')> and
  120. C<add('foobar')> should all give the same result.
  121.  
  122. The final message digest value is returned by the B<digest> operation
  123. as a 16-byte binary string. This operation delivers the result of
  124. B<add> operations since the last B<new> or B<reset> operation. Note
  125. that the B<digest> operation is effectively a destructive, read-once
  126. operation. Once it has been performed, the context must be B<reset>
  127. before being used to calculate another digest value.
  128.  
  129. Several convenience functions are also provided. The B<addfile>
  130. operation takes an open file-handle and reads it until end-of file in
  131. 1024 byte blocks adding the contents to the context. The file-handle
  132. can either be specified by name or passed as a type-glob reference, as
  133. shown in the examples below. The B<hexdigest> operation calls
  134. B<digest> and returns the result as a printable string of hexdecimal
  135. digits. This is exactly the same operation as performed by the
  136. B<unpack> operation in the examples below.
  137.  
  138. The B<hash> operation can act as either a static member function (ie
  139. you invoke it on the MD5 class as in the synopsis above) or as a
  140. normal virtual function. In both cases it performs the complete MD5
  141. cycle (reset, add, digest) on the supplied scalar value. This is
  142. convenient for handling small quantities of data. When invoked on the
  143. class a temporary context is created. When invoked through an already
  144. created context object, this context is used. The latter form is
  145. slightly more efficient. The B<hexhash> operation is analogous to
  146. B<hexdigest>.
  147.  
  148. =head1 EXAMPLES
  149.  
  150.     use MD5;
  151.     
  152.     $md5 = new MD5;
  153.     $md5->add('foo', 'bar');
  154.     $md5->add('baz');
  155.     $digest = $md5->digest();
  156.     
  157.     print("Digest is " . unpack("H*", $digest) . "\n");
  158.  
  159. The above example would print out the message
  160.  
  161.     Digest is 6df23dc03f9b54cc38a0fc1483df6e21
  162.  
  163. provided that the implementation is working correctly.
  164.  
  165. Remembering the Perl motto ("There's more than one way to do it"), the
  166. following should all give the same result:
  167.  
  168.     use MD5;
  169.     $md5 = new MD5;
  170.  
  171.     die "Can't open /etc/passwd ($!)\n" unless open(P, "/etc/passwd");
  172.  
  173.     seek(P, 0, 0);
  174.     $md5->reset;
  175.     $md5->addfile(P);
  176.     $d = $md5->hexdigest;
  177.     print "addfile (handle name) = $d\n";
  178.  
  179.     seek(P, 0, 0);
  180.     $md5->reset;
  181.     $md5->addfile(\*P);
  182.     $d = $md5->hexdigest;
  183.     print "addfile (type-glob reference) = $d\n";
  184.  
  185.     seek(P, 0, 0);
  186.     $md5->reset;
  187.     while (<P>)
  188.     {
  189.         $md5->add($_);
  190.     }
  191.     $d = $md5->hexdigest;
  192.     print "Line at a time = $d\n";
  193.  
  194.     seek(P, 0, 0);
  195.     $md5->reset;
  196.     $md5->add(<P>);
  197.     $d = $md5->hexdigest;
  198.     print "All lines at once = $d\n";
  199.  
  200.     seek(P, 0, 0);
  201.     $md5->reset;
  202.     while (read(P, $data, (rand % 128) + 1))
  203.     {
  204.         $md5->add($data);
  205.     }
  206.     $d = $md5->hexdigest;
  207.     print "Random chunks = $d\n";
  208.  
  209.     seek(P, 0, 0);
  210.     $md5->reset;
  211.     undef $/;
  212.     $data = <P>;
  213.     $d = $md5->hexhash($data);
  214.     print "Single string = $d\n";
  215.  
  216.     close(P);
  217.  
  218. =head1 NOTE
  219.  
  220. The MD5 extension may be redistributed under the same terms as Perl.
  221. The MD5 algorithm is defined in RFC1321. The basic C code implementing
  222. the algorithm is derived from that in the RFC and is covered by the
  223. following copyright:
  224.  
  225. =over 8
  226.  
  227. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  228. rights reserved.
  229.  
  230. License to copy and use this software is granted provided that it
  231. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  232. Algorithm" in all material mentioning or referencing this software
  233. or this function.
  234.  
  235. License is also granted to make and use derivative works provided
  236. that such works are identified as "derived from the RSA Data
  237. Security, Inc. MD5 Message-Digest Algorithm" in all material
  238. mentioning or referencing the derived work.
  239.  
  240. RSA Data Security, Inc. makes no representations concerning either
  241. the merchantability of this software or the suitability of this
  242. software for any particular purpose. It is provided "as is"
  243. without express or implied warranty of any kind.
  244.  
  245. These notices must be retained in any copies of any part of this
  246. documentation and/or software.
  247.  
  248. =back
  249.  
  250. This copyright does not prohibit distribution of any version of Perl
  251. containing this extension under the terms of the GNU or Artistic
  252. licences.
  253.  
  254. =head1 AUTHOR
  255.  
  256. The MD5 interface was written by Neil Winton
  257. (C<N.Winton@axion.bt.co.uk>).
  258.  
  259. =head1 SEE ALSO
  260.  
  261. perl(1).
  262.  
  263. =cut
  264.