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