Debian Codegraph @

upstream/mageia-cauldron/man3pm/Digest::MD5.3pm

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