Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.8 KiB

  1. package Digest;
  2. use strict;
  3. use vars qw($VERSION %MMAP $AUTOLOAD);
  4. $VERSION = "0.02";
  5. %MMAP = (
  6. "SHA-1" => "Digest::SHA1",
  7. "HMAC-MD5" => "Digest::HMAC_MD5",
  8. "HMAC-SHA-1" => "Digest::HMAC_SHA1",
  9. );
  10. sub new
  11. {
  12. shift; # class ignored
  13. my $algorithm = shift;
  14. my $class = $MMAP{$algorithm} || "Digest::$algorithm";
  15. no strict 'refs';
  16. unless (exists ${"$class\::"}{"VERSION"}) {
  17. eval "require $class";
  18. die $@ if $@;
  19. }
  20. $class->new(@_);
  21. }
  22. sub AUTOLOAD
  23. {
  24. my $class = shift;
  25. my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
  26. $class->new($algorithm, @_);
  27. }
  28. 1;
  29. __END__
  30. =head1 NAME
  31. Digest:: - Modules that calculate message digests
  32. =head1 SYNOPSIS
  33. $md2 = Digest->MD2;
  34. $md5 = Digest->MD5;
  35. $sha1 = Digest->SHA1;
  36. $sha1 = Digest->new("SHA-1");
  37. $hmac = Digest->HMAC_MD5($key);
  38. =head1 DESCRIPTION
  39. The C<Digest::> modules calculate digests, also called "fingerprints"
  40. or "hashes", of some data, called a message. The digest is some small
  41. fixed size string. The actual size of the digest depend of the
  42. algorithm used. The message is simply a sequence of arbitrary bytes.
  43. An important property of the digest algorithms is that the digest is
  44. I<likely> to change if the message change in some way. Another
  45. property is that digest functions are one-way functions, i.e. it
  46. should be I<hard> to find a message that correspond to some given
  47. digest. Algorithms differ in how "likely" and how "hard", as well as
  48. how efficient they are to compute.
  49. All C<Digest::> modules provide the same programming interface. A
  50. functional interface for simple use, as well as an object oriented
  51. interface that can handle messages of arbitrary length and which can
  52. read files directly.
  53. The digest can be delivered in three formats:
  54. =over 8
  55. =item I<binary>
  56. This is the most compact form, but it is not well suited for printing
  57. or embedding in places that can't handle arbitrary data.
  58. =item I<hex>
  59. A twice as long string of (lowercase) hexadecimal digits.
  60. =item I<base64>
  61. A string of portable printable characters. This is the base64 encoded
  62. representation of the digest with any trailing padding removed. The
  63. string will be about 30% longer than the binary version.
  64. L<MIME::Base64> tells you more about this encoding.
  65. =back
  66. The functional interface is simply importable functions with the same
  67. name as the algorithm. The functions take the message as argument and
  68. return the digest. Example:
  69. use Digest::MD5 qw(md5);
  70. $digest = md5($message);
  71. There are also versions of the functions with "_hex" or "_base64"
  72. appended to the name, which returns the digest in the indicated form.
  73. =head1 OO INTERFACE
  74. The following methods are available for all C<Digest::> modules:
  75. =over 4
  76. =item $ctx = Digest->XXX($arg,...)
  77. =item $ctx = Digest->new(XXX => $arg,...)
  78. =item $ctx = Digest::XXX->new($arg,...)
  79. The constructor returns some object that encapsulate the state of the
  80. message-digest algorithm. You can add data to the object and finally
  81. ask for the digest. The "XXX" should of course be replaced by the proper
  82. name of the digest algorithm you want to use.
  83. The two first forms are simply syntactic sugar which automatically
  84. load the right module on first use. The second form allow you to use
  85. algorithm names which contains letters which are not legal perl
  86. identifiers, e.g. "SHA-1".
  87. If new() is called as a instance method (i.e. $ctx->new) it will just
  88. reset the state the object to the state of a newly created object. No
  89. new object is created in this case, and the return value is the
  90. reference to the object (i.e. $ctx).
  91. =item $ctx->reset
  92. This is just an alias for $ctx->new.
  93. =item $ctx->add($data,...)
  94. The $data provided as argument are appended to the message we
  95. calculate the digest for. The return value is the $ctx object itself.
  96. =item $ctx->addfile($io_handle)
  97. The $io_handle is read until EOF and the content is appended to the
  98. message we calculate the digest for. The return value is the $ctx
  99. object itself.
  100. =item $ctx->digest
  101. Return the binary digest for the message.
  102. Note that the C<digest> operation is effectively a destructive,
  103. read-once operation. Once it has been performed, the $ctx object is
  104. automatically C<reset> and can be used to calculate another digest
  105. value.
  106. =item $ctx->hexdigest
  107. Same as $ctx->digest, but will return the digest in hexadecimal form.
  108. =item $ctx->b64digest
  109. Same as $ctx->digest, but will return the digest as a base64 encoded
  110. string.
  111. =back
  112. =head1 SEE ALSO
  113. L<Digest::MD5>, L<Digest::SHA1>, L<Digest::HMAC>, L<Digest::MD2>
  114. L<MIME::Base64>
  115. =head1 AUTHOR
  116. Gisle Aas <[email protected]>
  117. The C<Digest::> interface is based on the interface originally
  118. developed by Neil Winton for his C<MD5> module.
  119. =cut