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