home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _bb26be7a7e4d896e4f52bd605fb4a371 < prev    next >
Text File  |  2004-06-01  |  7KB  |  265 lines

  1. # $Id: MD4.pm,v 1.2 2001/07/30 21:58:13 mikem Exp $
  2. package Digest::MD4;
  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.1';
  19.  
  20. bootstrap Digest::MD4 $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.     return $self;
  47. }
  48.  
  49. sub hexdigest
  50. {
  51.     my ($self) = shift;
  52.  
  53.     unpack("H*", ($self->digest()));
  54. }
  55.  
  56. sub hash
  57. {
  58.     my ($self, $data) = @_;
  59.  
  60.     if (ref($self))
  61.     {
  62.     # This is an instance method call so reset the current context
  63.  
  64.     $self->reset();
  65.     }
  66.     else
  67.     {
  68.     # This is a static method invocation, create a temporary MD4 context
  69.  
  70.     $self = new Digest::MD4;
  71.     }
  72.  
  73.     # Now do the hash
  74.  
  75.     $self->add($data);
  76.     $self->digest();
  77. }
  78.  
  79. sub hexhash
  80. {
  81.     my ($self, $data) = @_;
  82.  
  83.     unpack("H*", ($self->hash($data)));
  84. }
  85.  
  86. # Autoload methods go after =cut, and are processed by the autosplit program.
  87.  
  88. 1;
  89. __END__
  90.  
  91. =head1 NAME
  92.  
  93. Digest::MD4 - Perl interface to the RSA Data Security Inc. MD4 Message-Digest Algorithm
  94.  
  95. =head1 SYNOPSIS
  96.  
  97.     use Digest::MD4;
  98.     
  99.     $context = new Digest::MD4;
  100.     $context->reset();
  101.     
  102.     $context->add(LIST);
  103.     $context->addfile(HANDLE);
  104.     
  105.     $digest = $context->digest();
  106.     $string = $context->hexdigest();
  107.  
  108.     $digest = Digest::MD4->hash(SCALAR);
  109.     $string = Digest::MD4->hexhash(SCALAR);
  110.  
  111. =head1 DESCRIPTION
  112.  
  113. The B<Digest::MD4> module allows you to use the RSA Data Security Inc. MD4
  114. Message Digest algorithm from within Perl programs.
  115.  
  116. A new MD4 context object is created with the B<new> operation.
  117. Multiple simultaneous digest contexts can be maintained, if desired.
  118. The context is updated with the B<add> operation which adds the
  119. strings contained in the I<LIST> parameter. Note, however, that
  120. C<add('foo', 'bar')>, C<add('foo')> followed by C<add('bar')> and
  121. C<add('foobar')> should all give the same result.
  122.  
  123. The final message digest value is returned by the B<digest> operation
  124. as a 16-byte binary string. This operation delivers the result of
  125. B<add> operations since the last B<new> or B<reset> operation. Note
  126. that the B<digest> operation is effectively a destructive, read-once
  127. operation. Once it has been performed, the context must be B<reset>
  128. before being used to calculate another digest value.
  129.  
  130. Several convenience functions are also provided. The B<addfile>
  131. operation takes an open file-handle and reads it until end-of file in
  132. 1024 byte blocks adding the contents to the context. The file-handle
  133. can either be specified by name or passed as a type-glob reference, as
  134. shown in the examples below. The B<hexdigest> operation calls
  135. B<digest> and returns the result as a printable string of hexdecimal
  136. digits. This is exactly the same operation as performed by the
  137. B<unpack> operation in the examples below.
  138.  
  139. The B<hash> operation can act as either a static member function (ie
  140. you invoke it on the MD4 class as in the synopsis above) or as a
  141. normal virtual function. In both cases it performs the complete MD4
  142. cycle (reset, add, digest) on the supplied scalar value. This is
  143. convenient for handling small quantities of data. When invoked on the
  144. class a temporary context is created. When invoked through an already
  145. created context object, this context is used. The latter form is
  146. slightly more efficient. The B<hexhash> operation is analogous to
  147. B<hexdigest>.
  148.  
  149. =head1 EXAMPLES
  150.  
  151.     use Digest::MD4;
  152.     
  153.     $md4 = new Digest::MD4;
  154.     $md4->add('foo', 'bar');
  155.     $md4->add('baz');
  156.     $digest = $md4->digest();
  157.     
  158.     print("Digest is " . unpack("H*", $digest) . "\n");
  159.  
  160. The above example would print out the message
  161.  
  162.     Digest is 6df23dc03f9b54cc38a0fc1483df6e21
  163.  
  164. provided that the implementation is working correctly.
  165.  
  166. Remembering the Perl motto ("There's more than one way to do it"), the
  167. following should all give the same result:
  168.  
  169.     use Digest::MD4;
  170.     $md4 = new Digest::MD4;
  171.  
  172.     die "Can't open /etc/passwd ($!)\n" unless open(P, "/etc/passwd");
  173.  
  174.     seek(P, 0, 0);
  175.     $md4->reset;
  176.     $md4->addfile(P);
  177.     $d = $md4->hexdigest;
  178.     print "addfile (handle name) = $d\n";
  179.  
  180.     seek(P, 0, 0);
  181.     $md4->reset;
  182.     $md4->addfile(\*P);
  183.     $d = $md4->hexdigest;
  184.     print "addfile (type-glob reference) = $d\n";
  185.  
  186.     seek(P, 0, 0);
  187.     $md4->reset;
  188.     while (<P>)
  189.     {
  190.         $md4->add($_);
  191.     }
  192.     $d = $md4->hexdigest;
  193.     print "Line at a time = $d\n";
  194.  
  195.     seek(P, 0, 0);
  196.     $md4->reset;
  197.     $md4->add(<P>);
  198.     $d = $md4->hexdigest;
  199.     print "All lines at once = $d\n";
  200.  
  201.     seek(P, 0, 0);
  202.     $md4->reset;
  203.     while (read(P, $data, (rand % 128) + 1))
  204.     {
  205.         $md4->add($data);
  206.     }
  207.     $d = $md4->hexdigest;
  208.     print "Random chunks = $d\n";
  209.  
  210.     seek(P, 0, 0);
  211.     $md4->reset;
  212.     undef $/;
  213.     $data = <P>;
  214.     $d = $md4->hexhash($data);
  215.     print "Single string = $d\n";
  216.  
  217.     close(P);
  218.  
  219. =head1 NOTE
  220.  
  221. The MD4 extension may be redistributed under the same terms as Perl.
  222. The MD4 algorithm is defined in RFC1320. The basic C code implementing
  223. the algorithm is derived from that in the RFC and is covered by the
  224. following copyright:
  225.  
  226. =over 8
  227.  
  228.    Copyright (C) 1990-2, RSA Data Security, Inc. All 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. MD4 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. MD4 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 MD4 interface was adapted by Mike McCauley
  257. (C<mikem@open.com.au>), based entirely on MD5-1.7, written by Neil Winton
  258. (C<N.Winton@axion.bt.co.uk>).
  259.  
  260. =head1 SEE ALSO
  261.  
  262. perl(1).
  263.  
  264. =cut
  265.