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

  1. package Digest::SHA1;
  2.  
  3. use strict;
  4. use vars qw($VERSION @ISA @EXPORT_OK);
  5.  
  6. $VERSION = '2.10';  # $Date: 2004/04/05 07:26:23 $
  7.  
  8. require Exporter;
  9. *import = \&Exporter::import;
  10. @EXPORT_OK = qw(sha1 sha1_hex sha1_base64 sha1_transform);
  11.  
  12. require DynaLoader;
  13. @ISA=qw(DynaLoader);
  14.  
  15. eval {
  16.     require Digest::base;
  17.     push(@ISA, 'Digest::base');
  18. };
  19. if ($@) {
  20.     my $err = $@;
  21.     *add_bits = sub { die $err };
  22. }
  23.  
  24. Digest::SHA1->bootstrap($VERSION);
  25.  
  26. 1;
  27. __END__
  28.  
  29. =head1 NAME
  30.  
  31. Digest::SHA1 - Perl interface to the SHA-1 algorithm
  32.  
  33. =head1 SYNOPSIS
  34.  
  35.  # Functional style
  36.  use Digest::SHA1  qw(sha1 sha1_hex sha1_base64);
  37.  
  38.  $digest = sha1($data);
  39.  $digest = sha1_hex($data);
  40.  $digest = sha1_base64($data);
  41.  $digest = sha1_transform($data);
  42.  
  43.  
  44.  # OO style
  45.  use Digest::SHA1;
  46.  
  47.  $sha1 = Digest::SHA1->new;
  48.  
  49.  $sha1->add($data);
  50.  $sha1->addfile(*FILE);
  51.  
  52.  $sha1_copy = $sha1->clone;
  53.  
  54.  $digest = $sha1->digest;
  55.  $digest = $sha1->hexdigest;
  56.  $digest = $sha1->b64digest;
  57.  $digest = $sha1->transform;
  58.  
  59. =head1 DESCRIPTION
  60.  
  61. The C<Digest::SHA1> module allows you to use the NIST SHA-1 message
  62. digest algorithm from within Perl programs.  The algorithm takes as
  63. input a message of arbitrary length and produces as output a 160-bit
  64. "fingerprint" or "message digest" of the input.
  65.  
  66. The C<Digest::SHA1> module provide a procedural interface for simple
  67. use, as well as an object oriented interface that can handle messages
  68. of arbitrary length and which can read files directly.
  69.  
  70. =head1 FUNCTIONS
  71.  
  72. The following functions can be exported from the C<Digest::SHA1>
  73. module.  No functions are exported by default.
  74.  
  75. =over 4
  76.  
  77. =item sha1($data,...)
  78.  
  79. This function will concatenate all arguments, calculate the SHA-1
  80. digest of this "message", and return it in binary form.  The returned
  81. string will be 20 bytes long.
  82.  
  83. The result of sha1("a", "b", "c") will be exactly the same as the
  84. result of sha1("abc").
  85.  
  86. =item sha1_hex($data,...)
  87.  
  88. Same as sha1(), but will return the digest in hexadecimal form.  The
  89. length of the returned string will be 40 and it will only contain
  90. characters from this set: '0'..'9' and 'a'..'f'.
  91.  
  92. =item sha1_base64($data,...)
  93.  
  94. Same as sha1(), but will return the digest as a base64 encoded string.
  95. The length of the returned string will be 27 and it will only contain
  96. characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' and
  97. '/'.
  98.  
  99. Note that the base64 encoded string returned is not padded to be a
  100. multiple of 4 bytes long.  If you want interoperability with other
  101. base64 encoded sha1 digests you might want to append the redundant
  102. string "=" to the result.
  103.  
  104. =item sha1_transform($data)
  105.  
  106. Implements the basic SHA1 transform on a 64 byte block. The $data
  107. argument and the returned $digest are in binary form. This algorithm
  108. is used in NIST FIPS 186-2
  109.  
  110. =back
  111.  
  112. =head1 METHODS
  113.  
  114. The object oriented interface to C<Digest::SHA1> is described in this
  115. section.  After a C<Digest::SHA1> object has been created, you will add
  116. data to it and finally ask for the digest in a suitable format.  A
  117. single object can be used to calculate multiple digests.
  118.  
  119. The following methods are provided:
  120.  
  121. =over 4
  122.  
  123. =item $sha1 = Digest::SHA1->new
  124.  
  125. The constructor returns a new C<Digest::SHA1> object which encapsulate
  126. the state of the SHA-1 message-digest algorithm.
  127.  
  128. If called as an instance method (i.e. $sha1->new) it will just reset the
  129. state the object to the state of a newly created object.  No new
  130. object is created in this case.
  131.  
  132. =item $sha1->reset
  133.  
  134. This is just an alias for $sha1->new.
  135.  
  136. =item $sha1->clone
  137.  
  138. This a copy of the $sha1 object. It is useful when you do not want to
  139. destroy the digests state, but need an intermediate value of the
  140. digest, e.g. when calculating digests iteratively on a continuous data
  141. stream.  Example:
  142.  
  143.     my $sha1 = Digest::SHA1->new;
  144.     while (<>) {
  145.     $sha1->add($_);
  146.     print "Line $.: ", $sha1->clone->hexdigest, "\n";
  147.     }
  148.  
  149. =item $sha1->add($data,...)
  150.  
  151. The $data provided as argument are appended to the message we
  152. calculate the digest for.  The return value is the $sha1 object itself.
  153.  
  154. All these lines will have the same effect on the state of the $sha1
  155. object:
  156.  
  157.     $sha1->add("a"); $sha1->add("b"); $sha1->add("c");
  158.     $sha1->add("a")->add("b")->add("c");
  159.     $sha1->add("a", "b", "c");
  160.     $sha1->add("abc");
  161.  
  162. =item $sha1->addfile($io_handle)
  163.  
  164. The $io_handle will be read until EOF and its content appended to the
  165. message we calculate the digest for.  The return value is the $sha1
  166. object itself.
  167.  
  168. The addfile() method will croak() if it fails reading data for some
  169. reason.  If it croaks it is unpredictable what the state of the $sha1
  170. object will be in. The addfile() method might have been able to read
  171. the file partially before it failed.  It is probably wise to discard
  172. or reset the $sha1 object if this occurs.
  173.  
  174. In most cases you want to make sure that the $io_handle is in
  175. C<binmode> before you pass it as argument to the addfile() method.
  176.  
  177. =item $sha1->add_bits($data, $nbits)
  178.  
  179. =item $sha1->add_bits($bitstring)
  180.  
  181. This implementation of SHA-1 only supports byte oriented input so you
  182. might only add bits as multiples of 8.  If you need bit level support
  183. please consider using the C<Digest::SHA> module instead.  The
  184. add_bits() method is provided here for compatibility with other digest
  185. implementations.  See L<Digest> for description of the arguments that
  186. add_bits() take.
  187.  
  188. =item $sha1->digest
  189.  
  190. Return the binary digest for the message.  The returned string will be
  191. 20 bytes long.
  192.  
  193. Note that the C<digest> operation is effectively a destructive,
  194. read-once operation. Once it has been performed, the C<Digest::SHA1>
  195. object is automatically C<reset> and can be used to calculate another
  196. digest value.  Call $sha1->clone->digest if you want to calculate the
  197. digest without reseting the digest state.
  198.  
  199. =item $sha1->hexdigest
  200.  
  201. Same as $sha1->digest, but will return the digest in hexadecimal
  202. form. The length of the returned string will be 40 and it will only
  203. contain characters from this set: '0'..'9' and 'a'..'f'.
  204.  
  205. =item $sha1->b64digest
  206.  
  207. Same as $sha1->digest, but will return the digest as a base64 encoded
  208. string.  The length of the returned string will be 27 and it will only
  209. contain characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+'
  210. and '/'.
  211.  
  212.  
  213. The base64 encoded string returned is not padded to be a multiple of 4
  214. bytes long.  If you want interoperability with other base64 encoded
  215. SHA-1 digests you might want to append the string "=" to the result.
  216.  
  217. =back
  218.  
  219. =head1 SEE ALSO
  220.  
  221. L<Digest>, L<Digest::HMAC_SHA1>, L<Digest::SHA>, L<Digest::MD5>
  222.  
  223. http://www.itl.nist.gov/fipspubs/fip180-1.htm
  224.  
  225. =head1 COPYRIGHT
  226.  
  227. This library is free software; you can redistribute it and/or
  228. modify it under the same terms as Perl itself.
  229.  
  230.  Copyright 1999-2003 Gisle Aas.
  231.  Copyright 1997 Uwe Hollerbach.
  232.  
  233. =head1 AUTHORS
  234.  
  235. Peter C. Gutmann,
  236. Uwe Hollerbach <uh@alumni.caltech.edu>,
  237. Gisle Aas <gisle@aas.no>
  238.  
  239. =cut
  240.