Update Digest to 1.03
[p5sagit/p5-mst-13.2.git] / lib / Digest.pm
1 package Digest;
2
3 use strict;
4 use vars qw($VERSION %MMAP $AUTOLOAD);
5
6 $VERSION = "1.03";
7
8 %MMAP = (
9   "SHA-1"      => ["Digest::SHA1", ["Digest::SHA", 1], ["Digest::SHA2", 1]],
10   "SHA-256"    => [["Digest::SHA", 256], ["Digest::SHA2", 256]],
11   "SHA-384"    => [["Digest::SHA", 384], ["Digest::SHA2", 384]],
12   "SHA-512"    => [["Digest::SHA", 512], ["Digest::SHA2", 512]],
13   "HMAC-MD5"   => "Digest::HMAC_MD5",
14   "HMAC-SHA-1" => "Digest::HMAC_SHA1",
15 );
16
17 sub new
18 {
19     shift;  # class ignored
20     my $algorithm = shift;
21     my $impl = $MMAP{$algorithm} || do {
22         $algorithm =~ s/\W+//;
23         "Digest::$algorithm";
24     };
25     $impl = [$impl] unless ref($impl);
26     my $err;
27     for  (@$impl) {
28         my $class = $_;
29         my @args;
30         ($class, @args) = @$class if ref($class);
31         no strict 'refs';
32         unless (exists ${"$class\::"}{"VERSION"}) {
33             eval "require $class";
34             if ($@) {
35                 $err ||= $@;
36                 next;
37             }
38         }
39         return $class->new(@args, @_);
40     }
41     die $err;
42 }
43
44 sub AUTOLOAD
45 {
46     my $class = shift;
47     my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
48     $class->new($algorithm, @_);
49 }
50
51 1;
52
53 __END__
54
55 =head1 NAME
56
57 Digest:: - Modules that calculate message digests
58
59 =head1 SYNOPSIS
60
61   $md2 = Digest->MD2;
62   $md5 = Digest->MD5;
63
64   $sha1 = Digest->SHA1;
65   $sha1 = Digest->new("SHA-1");
66
67   $hmac = Digest->HMAC_MD5($key);
68
69 =head1 DESCRIPTION
70
71 The C<Digest::> modules calculate digests, also called "fingerprints"
72 or "hashes", of some data, called a message.  The digest is (usually)
73 some small/fixed size string.  The actual size of the digest depend of
74 the algorithm used.  The message is simply a sequence of arbitrary
75 bytes or bits.
76
77 An important property of the digest algorithms is that the digest is
78 I<likely> to change if the message change in some way.  Another
79 property is that digest functions are one-way functions, i.e. it
80 should be I<hard> to find a message that correspond to some given
81 digest.  Algorithms differ in how "likely" and how "hard", as well as
82 how efficient they are to compute.
83
84 All C<Digest::> modules provide the same programming interface.  A
85 functional interface for simple use, as well as an object oriented
86 interface that can handle messages of arbitrary length and which can
87 read files directly.
88
89 The digest can be delivered in three formats:
90
91 =over 8
92
93 =item I<binary>
94
95 This is the most compact form, but it is not well suited for printing
96 or embedding in places that can't handle arbitrary data.
97
98 =item I<hex>
99
100 A twice as long string of (lowercase) hexadecimal digits.
101
102 =item I<base64>
103
104 A string of portable printable characters.  This is the base64 encoded
105 representation of the digest with any trailing padding removed.  The
106 string will be about 30% longer than the binary version.
107 L<MIME::Base64> tells you more about this encoding.
108
109 =back
110
111
112 The functional interface is simply importable functions with the same
113 name as the algorithm.  The functions take the message as argument and
114 return the digest.  Example:
115
116   use Digest::MD5 qw(md5);
117   $digest = md5($message);
118
119 There are also versions of the functions with "_hex" or "_base64"
120 appended to the name, which returns the digest in the indicated form.
121
122 =head1 OO INTERFACE
123
124 The following methods are available for all C<Digest::> modules:
125
126 =over 4
127
128 =item $ctx = Digest->XXX($arg,...)
129
130 =item $ctx = Digest->new(XXX => $arg,...)
131
132 =item $ctx = Digest::XXX->new($arg,...)
133
134 The constructor returns some object that encapsulate the state of the
135 message-digest algorithm.  You can add data to the object and finally
136 ask for the digest.  The "XXX" should of course be replaced by the proper
137 name of the digest algorithm you want to use.
138
139 The two first forms are simply syntactic sugar which automatically
140 load the right module on first use.  The second form allow you to use
141 algorithm names which contains letters which are not legal perl
142 identifiers, e.g. "SHA-1".
143
144 If new() is called as an instance method (i.e. $ctx->new) it will just
145 reset the state the object to the state of a newly created object.  No
146 new object is created in this case, and the return value is the
147 reference to the object (i.e. $ctx).
148
149 =item $other_ctx = $ctx->clone
150
151 The clone method creates a copy of the digest state object and returns
152 a reference to the copy.
153
154 =item $ctx->reset
155
156 This is just an alias for $ctx->new.
157
158 =item $ctx->add($data,...)
159
160 The $data provided as argument are appended to the message we
161 calculate the digest for.  The return value is the $ctx object itself.
162
163 =item $ctx->addfile($io_handle)
164
165 The $io_handle is read until EOF and the content is appended to the
166 message we calculate the digest for.  The return value is the $ctx
167 object itself.
168
169 =item $ctx->add_bits($data, $nbits)
170
171 =item $ctx->add_bits($bitstring)
172
173 The bits provided are appended to the message we calculate the digest
174 for.  The return value is the $ctx object itself.
175
176 The two argument form of add_bits() will add the first $nbits bits
177 from data.  For the last potentially partial byte only the high order
178 C<< $nbits % 8 >> bits are used.  If $nbits is greater than C<<
179 length($data) * 8 >>, then this method would do the same as C<<
180 $ctx->add($data) >>, i.e. $nbits is silently ignored.
181
182 The one argument form of add_bits() takes a $bitstring of "1" and "0"
183 chars as argument.  It's a shorthand for C<< $ctx->add_bits(pack("B*",
184 $bitstring), length($bitstring)) >>.
185
186 This example shows two calls that should have the same effect:
187
188    $ctx->add_bits("111100001010");
189    $ctx->add_bits("\xF0\xA0", 12);
190
191 Most digest algorithms are byte based.  For those it is not possible
192 to add bits that are not a multiple of 8, and the add_bits() method
193 will croak if you try.
194
195 =item $ctx->digest
196
197 Return the binary digest for the message.
198
199 Note that the C<digest> operation is effectively a destructive,
200 read-once operation. Once it has been performed, the $ctx object is
201 automatically C<reset> and can be used to calculate another digest
202 value.  Call $ctx->clone->digest if you want to calculate the digest
203 without reseting the digest state.
204
205 =item $ctx->hexdigest
206
207 Same as $ctx->digest, but will return the digest in hexadecimal form.
208
209 =item $ctx->b64digest
210
211 Same as $ctx->digest, but will return the digest as a base64 encoded
212 string.
213
214 =back
215
216 =head1 SEE ALSO
217
218 L<Digest::MD5>, L<Digest::SHA1>, L<Digest::HMAC>, L<Digest::MD2>
219
220 L<MIME::Base64>
221
222 =head1 AUTHOR
223
224 Gisle Aas <gisle@aas.no>
225
226 The C<Digest::> interface is based on the interface originally
227 developed by Neil Winton for his C<MD5> module.
228
229 =cut