Update MIME::Base64 and Digest::MD5 from the CPAN version.
[p5sagit/p5-mst-13.2.git] / ext / Digest / MD5 / MD5.pm
1 package Digest::MD5;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK);
5
6 $VERSION = '2.30';  # $Date: 2003/10/09 09:26:59 $
7
8 require Exporter;
9 *import = \&Exporter::import;
10 @EXPORT_OK = qw(md5 md5_hex md5_base64);
11
12 require DynaLoader;
13 @ISA=qw(DynaLoader);
14
15 eval {
16     Digest::MD5->bootstrap($VERSION);
17 };
18 if ($@) {
19     my $olderr = $@;
20     eval {
21         # Try to load the pure perl version
22         require Digest::Perl::MD5;
23
24         Digest::Perl::MD5->import(qw(md5 md5_hex md5_base64));
25         push(@ISA, "Digest::Perl::MD5");  # make OO interface work
26     };
27     if ($@) {
28         # restore the original error
29         die $olderr;
30     }
31 }
32 else {
33     *reset = \&new;
34 }
35
36 1;
37 __END__
38
39 =head1 NAME
40
41 Digest::MD5 - Perl interface to the MD5 Algorithm
42
43 =head1 SYNOPSIS
44
45  # Functional style
46  use Digest::MD5 qw(md5 md5_hex md5_base64);
47
48  $digest = md5($data);
49  $digest = md5_hex($data);
50  $digest = md5_base64($data);
51
52  # OO style
53  use Digest::MD5;
54
55  $ctx = Digest::MD5->new;
56
57  $ctx->add($data);
58  $ctx->addfile(*FILE);
59
60  $digest = $ctx->digest;
61  $digest = $ctx->hexdigest;
62  $digest = $ctx->b64digest;
63
64 =head1 DESCRIPTION
65
66 The C<Digest::MD5> module allows you to use the RSA Data Security
67 Inc. MD5 Message Digest algorithm from within Perl programs.  The
68 algorithm takes as input a message of arbitrary length and produces as
69 output a 128-bit "fingerprint" or "message digest" of the input.
70
71 The C<Digest::MD5> module provide a procedural interface for simple
72 use, as well as an object oriented interface that can handle messages
73 of arbitrary length and which can read files directly.
74
75 =head1 FUNCTIONS
76
77 The following functions are provided by the C<Digest::MD5> module.
78 None of these functions are exported by default.
79
80 =over 4
81
82 =item md5($data,...)
83
84 This function will concatenate all arguments, calculate the MD5 digest
85 of this "message", and return it in binary form.  The returned string
86 will be 16 bytes long.
87
88 The result of md5("a", "b", "c") will be exactly the same as the
89 result of md5("abc").
90
91 =item md5_hex($data,...)
92
93 Same as md5(), but will return the digest in hexadecimal form. The
94 length of the returned string will be 32 and it will only contain
95 characters from this set: '0'..'9' and 'a'..'f'.
96
97 =item md5_base64($data,...)
98
99 Same as md5(), but will return the digest as a base64 encoded string.
100 The length of the returned string will be 22 and it will only contain
101 characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+' and
102 '/'.
103
104 Note that the base64 encoded string returned is not padded to be a
105 multiple of 4 bytes long.  If you want interoperability with other
106 base64 encoded md5 digests you might want to append the redundant
107 string "==" to the result.
108
109 =back
110
111 =head1 METHODS
112
113 The object oriented interface to C<Digest::MD5> is described in this
114 section.  After a C<Digest::MD5> object has been created, you will add
115 data to it and finally ask for the digest in a suitable format.  A
116 single object can be used to calculate multiple digests.
117
118 The following methods are provided:
119
120 =over 4
121
122 =item $md5 = Digest::MD5->new
123
124 The constructor returns a new C<Digest::MD5> object which encapsulate
125 the state of the MD5 message-digest algorithm.
126
127 If called as an instance method (i.e. $md5->new) it will just reset the
128 state the object to the state of a newly created object.  No new
129 object is created in this case.
130
131 =item $md5->reset
132
133 This is just an alias for $md5->new.
134
135 =item $md5->clone
136
137 This a copy of the $md5 object. It is useful when you do not want to
138 destroy the digests state, but need an intermediate value of the
139 digest, e.g. when calculating digests iteratively on a continuous data
140 stream.  Example:
141
142     my $md5 = Digest::MD5->new;
143     while (<>) {
144         $md5->add($_);
145         print "Line $.: ", $md5->clone->hexdigest, "\n";
146     }
147
148 =item $md5->add($data,...)
149
150 The $data provided as argument are appended to the message we
151 calculate the digest for.  The return value is the $md5 object itself.
152
153 All these lines will have the same effect on the state of the $md5
154 object:
155
156     $md5->add("a"); $md5->add("b"); $md5->add("c");
157     $md5->add("a")->add("b")->add("c");
158     $md5->add("a", "b", "c");
159     $md5->add("abc");
160
161 =item $md5->addfile($io_handle)
162
163 The $io_handle will be read until EOF and its content appended to the
164 message we calculate the digest for.  The return value is the $md5
165 object itself.
166
167 The addfile() method will croak() if it fails reading data for some
168 reason.  If it croaks it is unpredictable what the state of the $md5
169 object will be in. The addfile() method might have been able to read
170 the file partially before it failed.  It is probably wise to discard
171 or reset the $md5 object if this occurs.
172
173 In most cases you want to make sure that the $io_handle is in
174 C<binmode> before you pass it as argument to the addfile() method.
175
176 =item $md5->digest
177
178 Return the binary digest for the message.  The returned string will be
179 16 bytes long.
180
181 Note that the C<digest> operation is effectively a destructive,
182 read-once operation. Once it has been performed, the C<Digest::MD5>
183 object is automatically C<reset> and can be used to calculate another
184 digest value.  Call $md5->clone->digest if you want to calculate the
185 digest without reseting the digest state.
186
187 =item $md5->hexdigest
188
189 Same as $md5->digest, but will return the digest in hexadecimal
190 form. The length of the returned string will be 32 and it will only
191 contain characters from this set: '0'..'9' and 'a'..'f'.
192
193 =item $md5->b64digest
194
195 Same as $md5->digest, but will return the digest as a base64 encoded
196 string.  The length of the returned string will be 22 and it will only
197 contain characters from this set: 'A'..'Z', 'a'..'z', '0'..'9', '+'
198 and '/'.
199
200
201 The base64 encoded string returned is not padded to be a multiple of 4
202 bytes long.  If you want interoperability with other base64 encoded
203 md5 digests you might want to append the string "==" to the result.
204
205 =back
206
207
208 =head1 EXAMPLES
209
210 The simplest way to use this library is to import the md5_hex()
211 function (or one of its cousins):
212
213     use Digest::MD5 qw(md5_hex);
214     print "Digest is ", md5_hex("foobarbaz"), "\n";
215
216 The above example would print out the message:
217
218     Digest is 6df23dc03f9b54cc38a0fc1483df6e21
219
220 The same checksum can also be calculated in OO style:
221
222     use Digest::MD5;
223     
224     $md5 = Digest::MD5->new;
225     $md5->add('foo', 'bar');
226     $md5->add('baz');
227     $digest = $md5->hexdigest;
228     
229     print "Digest is $digest\n";
230
231 With OO style you can break the message arbitrary.  This means that we
232 are no longer limited to have space for the whole message in memory, i.e.
233 we can handle messages of any size.
234
235 This is useful when calculating checksum for files:
236
237     use Digest::MD5;
238
239     my $file = shift || "/etc/passwd";
240     open(FILE, $file) or die "Can't open '$file': $!";
241     binmode(FILE);
242
243     $md5 = Digest::MD5->new;
244     while (<FILE>) {
245         $md5->add($_);
246     }
247     close(FILE);
248     print $md5->b64digest, " $file\n";
249
250 Or we can use the addfile method for more efficient reading of
251 the file:
252
253     use Digest::MD5;
254
255     my $file = shift || "/etc/passwd";
256     open(FILE, $file) or die "Can't open '$file': $!";
257     binmode(FILE);
258
259     print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
260
261 Perl 5.8 support Unicode characters in strings.  Since the MD5
262 algorithm is only defined for strings of bytes, it can not be used on
263 strings that contains chars with ordinal number above 255.  The MD5
264 functions and methods will croak if you try to feed them such input
265 data:
266
267     use Digest::MD5 qw(md5_hex);
268
269     my $str = "abc\x{300}";
270     print md5_hex($str), "\n";  # croaks
271     # Wide character in subroutine entry
272
273 What you can do is calculate the MD5 checksum of the UTF-8
274 representation of such strings.  This is achieved by filtering the
275 string through encode_utf8() function:
276
277     use Digest::MD5 qw(md5_hex);
278     use Encode qw(encode_utf8);
279
280     my $str = "abc\x{300}";
281     print md5_hex(encode_utf8($str)), "\n";
282     # 8c2d46911f3f5a326455f0ed7a8ed3b3
283
284 =head1 SEE ALSO
285
286 L<Digest>,
287 L<Digest::MD2>,
288 L<Digest::SHA1>,
289 L<Digest::HMAC>
290
291 L<md5sum(1)>
292
293 RFC 1321
294
295 =head1 COPYRIGHT
296
297 This library is free software; you can redistribute it and/or
298 modify it under the same terms as Perl itself.
299
300  Copyright 1998-2003 Gisle Aas.
301  Copyright 1995-1996 Neil Winton.
302  Copyright 1991-1992 RSA Data Security, Inc.
303
304 The MD5 algorithm is defined in RFC 1321. This implementation is
305 derived from the reference C code in RFC 1321 which is covered by
306 the following copyright statement:
307
308 =over 4
309
310 =item
311
312 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
313 rights reserved.
314
315 License to copy and use this software is granted provided that it
316 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
317 Algorithm" in all material mentioning or referencing this software
318 or this function.
319
320 License is also granted to make and use derivative works provided
321 that such works are identified as "derived from the RSA Data
322 Security, Inc. MD5 Message-Digest Algorithm" in all material
323 mentioning or referencing the derived work.
324
325 RSA Data Security, Inc. makes no representations concerning either
326 the merchantability of this software or the suitability of this
327 software for any particular purpose. It is provided "as is"
328 without express or implied warranty of any kind.
329
330 These notices must be retained in any copies of any part of this
331 documentation and/or software.
332
333 =back
334
335 This copyright does not prohibit distribution of any version of Perl
336 containing this extension under the terms of the GNU or Artistic
337 licenses.
338
339 =head1 AUTHORS
340
341 The original C<MD5> interface was written by Neil Winton
342 (C<N.Winton@axion.bt.co.uk>).
343
344 The C<Digest::MD5> module is written by Gisle Aas <gisle@ActiveState.com>.
345
346 =cut