Introduce d_u32align / U32_REQUIRES_ALIGNMENT, needed for
[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
a019d632 6$VERSION = '2.13';
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 ($@) {
19 # Try to load the pure perl version
20 require Digest::Perl::MD5;
21
22 Digest::Perl::MD5->import(qw(md5 md5_hex md5_base64));
23 push(@ISA, "Digest::Perl::MD5"); # make OO interface work
24}
25else {
26 *reset = \&new;
27}
28
291;
30__END__
31
32=head1 NAME
33
34Digest::MD5 - Perl interface to the MD5 Algorithm
35
36=head1 SYNOPSIS
37
38 # Functional style
39 use Digest::MD5 qw(md5 md5_hex md5_base64);
40
41 $digest = md5($data);
42 $digest = md5_hex($data);
43 $digest = md5_base64($data);
44
45 # OO style
46 use Digest::MD5;
47
48 $ctx = Digest::MD5->new;
49
50 $ctx->add($data);
51 $ctx->addfile(*FILE);
52
53 $digest = $ctx->digest;
54 $digest = $ctx->hexdigest;
55 $digest = $ctx->b64digest;
56
57=head1 DESCRIPTION
58
59The C<Digest::MD5> module allows you to use the RSA Data Security
60Inc. MD5 Message Digest algorithm from within Perl programs. The
61algorithm takes as input a message of arbitrary length and produces as
62output a 128-bit "fingerprint" or "message digest" of the input.
63
64The C<Digest::MD5> module provide a procedural interface for simple
65use, as well as an object oriented interface that can handle messages
66of arbitrary length and which can read files directly.
67
68A binary digest will be 16 bytes long. A hex digest will be 32
69characters long. A base64 digest will be 22 characters long.
70
71=head1 FUNCTIONS
72
73The following functions can be exported from the C<Digest::MD5>
74module. No functions are exported by default.
75
76=over 4
77
78=item md5($data,...)
79
80This function will concatenate all arguments, calculate the MD5 digest
81of this "message", and return it in binary form.
82
83=item md5_hex($data,...)
84
85Same as md5(), but will return the digest in hexadecimal form.
86
87=item md5_base64($data,...)
88
89Same as md5(), but will return the digest as a base64 encoded string.
90
91=back
92
93=head1 METHODS
94
95The following methods are available:
96
97=over 4
98
99=item $md5 = Digest::MD5->new
100
101The constructor returns a new C<Digest::MD5> object which encapsulate
102the state of the MD5 message-digest algorithm. You can add data to
103the object and finally ask for the digest.
104
105If called as a instance method (i.e. $md5->new) it will just reset the
106state the object to the state of a newly created object. No new
107object is created in this case.
108
109=item $md5->reset
110
111This is just an alias for $md5->new.
112
113=item $md5->add($data,...)
114
115The $data provided as argument are appended to the message we
116calculate the digest for. The return value is the $md5 object itself.
117
118=item $md5->addfile($io_handle)
119
120The $io_handle is read until EOF and the content is appended to the
121message we calculate the digest for. The return value is the $md5
122object itself.
123
124In most cases you want to make sure that the $io_handle is set up to
125be in binmode().
126
127=item $md5->digest
128
129Return the binary digest for the message.
130
131Note that the C<digest> operation is effectively a destructive,
132read-once operation. Once it has been performed, the C<Digest::MD5>
133object is automatically C<reset> and can be used to calculate another
134digest value.
135
136=item $md5->hexdigest
137
138Same as $md5->digest, but will return the digest in hexadecimal form.
139
140=item $md5->b64digest
141
142Same as $md5->digest, but will return the digest as a base64 encoded
143string.
144
145=back
146
147
148=head1 EXAMPLES
149
150The simplest way to use this library is to import the md5_hex()
151function (or one of its cousins):
152
153 use Digest::MD5 qw(md5_hex);
154 print "Digest is ", md5_hex("foobarbaz"), "\n";
155
156The above example would print out the message
157
158 Digest is 6df23dc03f9b54cc38a0fc1483df6e21
159
160provided that the implementation is working correctly. The same
161checksum can also be calculated in OO style:
162
163 use Digest::MD5;
164
165 $md5 = Digest::MD5->new;
166 $md5->add('foo', 'bar');
167 $md5->add('baz');
168 $digest = $md5->hexdigest;
169
170 print "Digest is $digest\n";
171
172With OO style you can break the message arbitrary. This means that we
173are no longer limited to have space for the whole message in memory, i.e.
174we can handle messages of any size.
175
176This is useful when calculating checksum for files:
177
178 use Digest::MD5;
179
180 my $file = shift || "/etc/passwd";
181 open(FILE, $file) or die "Can't open '$file': $!";
182 binmode(FILE);
183
184 $md5 = Digest::MD5->new;
185 while (<FILE>) {
186 $md5->add($_);
187 }
188 close(FILE);
189 print $md5->b64digest, " $file\n";
190
191Or we can use the builtin addfile method for more efficient reading of
192the file:
193
194 use Digest::MD5;
195
196 my $file = shift || "/etc/passwd";
197 open(FILE, $file) or die "Can't open '$file': $!";
198 binmode(FILE);
199
200 print Digest::MD5->new->addfile(*FILE)->hexdigest, " $file\n";
201
202=head1 SEE ALSO
203
204L<Digest>,
205L<Digest::MD2>,
206L<Digest::SHA1>,
207L<Digest::HMAC>
208
209L<md5sum(1)>
210
211RFC 1321
212
213=head1 COPYRIGHT
214
215This library is free software; you can redistribute it and/or
216modify it under the same terms as Perl itself.
217
218 Copyright 1998-2000 Gisle Aas.
219 Copyright 1995-1996 Neil Winton.
220 Copyright 1991-1992 RSA Data Security, Inc.
221
222The MD5 algorithm is defined in RFC 1321. The basic C code
223implementing the algorithm is derived from that in the RFC and is
224covered by the following copyright:
225
226=over 4
227
228=item
229
230Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
231rights reserved.
232
233License to copy and use this software is granted provided that it
234is identified as the "RSA Data Security, Inc. MD5 Message-Digest
235Algorithm" in all material mentioning or referencing this software
236or this function.
237
238License is also granted to make and use derivative works provided
239that such works are identified as "derived from the RSA Data
240Security, Inc. MD5 Message-Digest Algorithm" in all material
241mentioning or referencing the derived work.
242
243RSA Data Security, Inc. makes no representations concerning either
244the merchantability of this software or the suitability of this
245software for any particular purpose. It is provided "as is"
246without express or implied warranty of any kind.
247
248These notices must be retained in any copies of any part of this
249documentation and/or software.
250
251=back
252
253This copyright does not prohibit distribution of any version of Perl
254containing this extension under the terms of the GNU or Artistic
255licenses.
256
257=head1 AUTHORS
258
259The original MD5 interface was written by Neil Winton
260(C<N.Winton@axion.bt.co.uk>).
261
262This release was made by Gisle Aas <gisle@ActiveState.com>
263
264=cut