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