Make the :bytes conditional on PerlIO.
[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
77e6095e 6$VERSION = '2.23'; # $Date: 2003/01/19 04:42:15 $
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
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
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
75A binary digest will be 16 bytes long. A hex digest will be 32
76characters long. A base64 digest will be 22 characters long.
77
78=head1 FUNCTIONS
79
80The following functions can be exported from the C<Digest::MD5>
81module. No functions are exported by default.
82
83=over 4
84
85=item md5($data,...)
86
87This function will concatenate all arguments, calculate the MD5 digest
88of this "message", and return it in binary form.
89
90=item md5_hex($data,...)
91
92Same as md5(), but will return the digest in hexadecimal form.
93
94=item md5_base64($data,...)
95
96Same as md5(), but will return the digest as a base64 encoded string.
97
db2a39d5 98The base64 encoded string returned is not padded to be a multiple of 4
99bytes long. If you want interoperability with other base64 encoded
100md5 digests you might want to append the string "==" to the result.
101
3357b1b1 102=back
103
104=head1 METHODS
105
106The following methods are available:
107
108=over 4
109
110=item $md5 = Digest::MD5->new
111
112The constructor returns a new C<Digest::MD5> object which encapsulate
113the state of the MD5 message-digest algorithm. You can add data to
114the object and finally ask for the digest.
115
d1be9408 116If called as an instance method (i.e. $md5->new) it will just reset the
3357b1b1 117state the object to the state of a newly created object. No new
118object is created in this case.
119
f62a1bde 120=item $md5->clone
121
122This is a copy constructor returning a clone of the $md5 object. It is
123useful when you do not want to destroy the digests state, but need an
124intermediate value of the digest, e.g. when calculating digests
125iteratively on a continuous data stream in order to obtain a copy which
126may be destroyed.
127
3357b1b1 128=item $md5->reset
129
130This is just an alias for $md5->new.
131
132=item $md5->add($data,...)
133
134The $data provided as argument are appended to the message we
135calculate the digest for. The return value is the $md5 object itself.
136
137=item $md5->addfile($io_handle)
138
139The $io_handle is read until EOF and the content is appended to the
140message we calculate the digest for. The return value is the $md5
141object itself.
142
143In most cases you want to make sure that the $io_handle is set up to
144be in binmode().
145
146=item $md5->digest
147
148Return the binary digest for the message.
149
150Note that the C<digest> operation is effectively a destructive,
151read-once operation. Once it has been performed, the C<Digest::MD5>
152object is automatically C<reset> and can be used to calculate another
f62a1bde 153digest value. Call $md5->clone->digest if you want to calculate the
154digest without reseting the digest state.
3357b1b1 155
156=item $md5->hexdigest
157
158Same as $md5->digest, but will return the digest in hexadecimal form.
159
160=item $md5->b64digest
161
162Same as $md5->digest, but will return the digest as a base64 encoded
163string.
164
db2a39d5 165The base64 encoded string returned is not padded to be a multiple of 4
166bytes long. If you want interoperability with other base64 encoded
167md5 digests you might want to append the string "==" to the result.
168
3357b1b1 169=back
170
171
172=head1 EXAMPLES
173
174The simplest way to use this library is to import the md5_hex()
175function (or one of its cousins):
176
177 use Digest::MD5 qw(md5_hex);
178 print "Digest is ", md5_hex("foobarbaz"), "\n";
179
180The above example would print out the message
181
182 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
183
184provided that the implementation is working correctly. The same
185checksum can also be calculated in OO style:
186
187 use Digest::MD5;
188
189 $md5 = Digest::MD5->new;
190 $md5->add('foo', 'bar');
191 $md5->add('baz');
192 $digest = $md5->hexdigest;
193
194 print "Digest is $digest\n";
195
196With OO style you can break the message arbitrary. This means that we
197are no longer limited to have space for the whole message in memory, i.e.
198we can handle messages of any size.
199
200This is useful when calculating checksum for files:
201
202 use Digest::MD5;
203
204 my $file = shift || "/etc/passwd";
205 open(FILE, $file) or die "Can't open '$file': $!";
206 binmode(FILE);
207
208 $md5 = Digest::MD5->new;
209 while (<FILE>) {
210 $md5->add($_);
211 }
212 close(FILE);
213 print $md5->b64digest, " $file\n";
214
215Or we can use the builtin addfile method for more efficient reading of
216the file:
217
218 use Digest::MD5;
219
220 my $file = shift || "/etc/passwd";
221 open(FILE, $file) or die "Can't open '$file': $!";
222 binmode(FILE);
223
224 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
225
9a03235d 226Perl 5.8 support Unicode characters in strings. Since the MD5
227algorithm is only defined for strings of bytes, it can not be used on
228strings that contains chars with ordinal number above 255. The MD5
229functions and methods will croak if you try to feed them such input
230data:
231
232 use Digest::MD5 qw(md5_hex);
233
234 my $str = "abc\x{300}";
235 print md5_hex($str), "\n"; # croaks
236 # Wide character in subroutine entry
237
238What you can do is calculate the MD5 checksum of the UTF-8
239representation of such strings. This is achieved by filtering the
240string through encode_utf8() function:
241
242 use Digest::MD5 qw(md5_hex);
243 use Encode qw(encode_utf8);
244
245 my $str = "abc\x{300}";
246 print md5_hex(encode_utf8($str)), "\n";
247 # 8c2d46911f3f5a326455f0ed7a8ed3b3
248
3357b1b1 249=head1 SEE ALSO
250
251L<Digest>,
252L<Digest::MD2>,
253L<Digest::SHA1>,
254L<Digest::HMAC>
255
256L<md5sum(1)>
257
258RFC 1321
259
260=head1 COPYRIGHT
261
262This library is free software; you can redistribute it and/or
263modify it under the same terms as Perl itself.
264
f62a1bde 265 Copyright 1998-2003 Gisle Aas.
3357b1b1 266 Copyright 1995-1996 Neil Winton.
267 Copyright 1991-1992 RSA Data Security, Inc.
268
269The MD5 algorithm is defined in RFC 1321. The basic C code
270implementing the algorithm is derived from that in the RFC and is
271covered by the following copyright:
272
273=over 4
274
275=item
276
277Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
278rights reserved.
279
280License to copy and use this software is granted provided that it
281is identified as the "RSA Data Security, Inc. MD5 Message-Digest
282Algorithm" in all material mentioning or referencing this software
283or this function.
284
285License is also granted to make and use derivative works provided
286that such works are identified as "derived from the RSA Data
287Security, Inc. MD5 Message-Digest Algorithm" in all material
288mentioning or referencing the derived work.
289
290RSA Data Security, Inc. makes no representations concerning either
291the merchantability of this software or the suitability of this
292software for any particular purpose. It is provided "as is"
293without express or implied warranty of any kind.
294
295These notices must be retained in any copies of any part of this
296documentation and/or software.
297
298=back
299
300This copyright does not prohibit distribution of any version of Perl
301containing this extension under the terms of the GNU or Artistic
302licenses.
303
304=head1 AUTHORS
305
306The original MD5 interface was written by Neil Winton
307(C<N.Winton@axion.bt.co.uk>).
308
309This release was made by Gisle Aas <gisle@ActiveState.com>
310
311=cut