Debian Codegraph @

upstream/debian-unstable/man3/Digest.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (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 .\"
57 .IX Title "Digest 3perl"
58 .TH Digest 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Digest \- Modules that calculate message digests
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 5
68 \& $md5 = Digest\->new("MD5");
69 \& $sha1 = Digest\->new("SHA\-1");
70 \& $sha256 = Digest\->new("SHA\-256");
71 \& $sha384 = Digest\->new("SHA\-384");
72 \& $sha512 = Digest\->new("SHA\-512");
73 \&
74 \& $hmac = Digest\->HMAC_MD5($key);
75 .Ve
76 .SH DESCRIPTION
77 .IX Header "DESCRIPTION"
78 The \f(CW\*(C`Digest::\*(C'\fR modules calculate digests, also called "fingerprints"
79 or "hashes", of some data, called a message. The digest is (usually)
80 some small/fixed size string. The actual size of the digest depend of
81 the algorithm used. The message is simply a sequence of arbitrary
82 bytes or bits.
83 .PP
84 An important property of the digest algorithms is that the digest is
85 \&\fIlikely\fR to change if the message change in some way. Another
86 property is that digest functions are one-way functions, that is it
87 should be \fIhard\fR to find a message that correspond to some given
88 digest. Algorithms differ in how "likely" and how "hard", as well as
89 how efficient they are to compute.
90 .PP
91 Note that the properties of the algorithms change over time, as the
92 algorithms are analyzed and machines grow faster. If your application
93 for instance depends on it being "impossible" to generate the same
94 digest for a different message it is wise to make it easy to plug in
95 stronger algorithms as the one used grow weaker. Using the interface
96 documented here should make it easy to change algorithms later.
97 .PP
98 All \f(CW\*(C`Digest::\*(C'\fR modules provide the same programming interface. A
99 functional interface for simple use, as well as an object oriented
100 interface that can handle messages of arbitrary length and which can
101 read files directly.
102 .PP
103 The digest can be delivered in three formats:
104 .IP \fIbinary\fR 8
105 .IX Item "binary"
106 This is the most compact form, but it is not well suited for printing
107 or embedding in places that can't handle arbitrary data.
108 .IP \fIhex\fR 8
109 .IX Item "hex"
110 A twice as long string of lowercase hexadecimal digits.
111 .IP \fIbase64\fR 8
112 .IX Item "base64"
113 A string of portable printable characters. This is the base64 encoded
114 representation of the digest with any trailing padding removed. The
115 string will be about 30% longer than the binary version.
116 MIME::Base64 tells you more about this encoding.
117 .PP
118 The functional interface is simply importable functions with the same
119 name as the algorithm. The functions take the message as argument and
120 return the digest. Example:
121 .PP
122 .Vb 2
123 \& use Digest::MD5 qw(md5);
124 \& $digest = md5($message);
125 .Ve
126 .PP
127 There are also versions of the functions with "_hex" or "_base64"
128 appended to the name, which returns the digest in the indicated form.
129 .SH "OO INTERFACE"
130 .IX Header "OO INTERFACE"
131 The following methods are available for all \f(CW\*(C`Digest::\*(C'\fR modules:
132 .ie n .IP "$ctx = Digest\->XXX($arg,...)" 4
133 .el .IP "\f(CW$ctx\fR = Digest\->XXX($arg,...)" 4
134 .IX Item "$ctx = Digest->XXX($arg,...)"
135 .PD 0
136 .ie n .IP "$ctx = Digest\->new(XXX => $arg,...)" 4
137 .el .IP "\f(CW$ctx\fR = Digest\->new(XXX => \f(CW$arg\fR,...)" 4
138 .IX Item "$ctx = Digest->new(XXX => $arg,...)"
139 .ie n .IP "$ctx = Digest::XXX\->new($arg,...)" 4
140 .el .IP "\f(CW$ctx\fR = Digest::XXX\->new($arg,...)" 4
141 .IX Item "$ctx = Digest::XXX->new($arg,...)"
142 .PD
143 The constructor returns some object that encapsulate the state of the
144 message-digest algorithm. You can add data to the object and finally
145 ask for the digest. The "XXX" should of course be replaced by the proper
146 name of the digest algorithm you want to use.
147 .Sp
148 The two first forms are simply syntactic sugar which automatically
149 load the right module on first use. The second form allow you to use
150 algorithm names which contains letters which are not legal perl
151 identifiers, e.g. "SHA\-1". If no implementation for the given algorithm
152 can be found, then an exception is raised.
153 .Sp
154 To know what arguments (if any) the constructor takes (the \f(CW\*(C`$args,...\*(C'\fR above)
155 consult the docs for the specific digest implementation.
156 .Sp
157 If \fBnew()\fR is called as an instance method (i.e. \f(CW$ctx\fR\->new) it will just
158 reset the state the object to the state of a newly created object. No
159 new object is created in this case, and the return value is the
160 reference to the object (i.e. \f(CW$ctx\fR).
161 .ie n .IP "$other_ctx = $ctx\->clone" 4
162 .el .IP "\f(CW$other_ctx\fR = \f(CW$ctx\fR\->clone" 4
163 .IX Item "$other_ctx = $ctx->clone"
164 The clone method creates a copy of the digest state object and returns
165 a reference to the copy.
166 .ie n .IP $ctx\->reset 4
167 .el .IP \f(CW$ctx\fR\->reset 4
168 .IX Item "$ctx->reset"
169 This is just an alias for \f(CW$ctx\fR\->new.
170 .ie n .IP "$ctx\->add( $data )" 4
171 .el .IP "\f(CW$ctx\fR\->add( \f(CW$data\fR )" 4
172 .IX Item "$ctx->add( $data )"
173 .PD 0
174 .ie n .IP "$ctx\->add( $chunk1, $chunk2, ... )" 4
175 .el .IP "\f(CW$ctx\fR\->add( \f(CW$chunk1\fR, \f(CW$chunk2\fR, ... )" 4
176 .IX Item "$ctx->add( $chunk1, $chunk2, ... )"
177 .PD
178 The string value of the \f(CW$data\fR provided as argument is appended to the
179 message we calculate the digest for. The return value is the \f(CW$ctx\fR
180 object itself.
181 .Sp
182 If more arguments are provided then they are all appended to the
183 message, thus all these lines will have the same effect on the state
184 of the \f(CW$ctx\fR object:
185 .Sp
186 .Vb 4
187 \& $ctx\->add("a"); $ctx\->add("b"); $ctx\->add("c");
188 \& $ctx\->add("a")\->add("b")\->add("c");
189 \& $ctx\->add("a", "b", "c");
190 \& $ctx\->add("abc");
191 .Ve
192 .Sp
193 Most algorithms are only defined for strings of bytes and this method
194 might therefore croak if the provided arguments contain chars with
195 ordinal number above 255.
196 .ie n .IP "$ctx\->addfile( $io_handle )" 4
197 .el .IP "\f(CW$ctx\fR\->addfile( \f(CW$io_handle\fR )" 4
198 .IX Item "$ctx->addfile( $io_handle )"
199 The \f(CW$io_handle\fR is read until EOF and the content is appended to the
200 message we calculate the digest for. The return value is the \f(CW$ctx\fR
201 object itself.
202 .Sp
203 The \fBaddfile()\fR method will \fBcroak()\fR if it fails reading data for some
204 reason. If it croaks it is unpredictable what the state of the \f(CW$ctx\fR
205 object will be in. The \fBaddfile()\fR method might have been able to read
206 the file partially before it failed. It is probably wise to discard
207 or reset the \f(CW$ctx\fR object if this occurs.
208 .Sp
209 In most cases you want to make sure that the \f(CW$io_handle\fR is in
210 "binmode" before you pass it as argument to the \fBaddfile()\fR method.
211 .ie n .IP "$ctx\->add_bits( $data, $nbits )" 4
212 .el .IP "\f(CW$ctx\fR\->add_bits( \f(CW$data\fR, \f(CW$nbits\fR )" 4
213 .IX Item "$ctx->add_bits( $data, $nbits )"
214 .PD 0
215 .ie n .IP "$ctx\->add_bits( $bitstring )" 4
216 .el .IP "\f(CW$ctx\fR\->add_bits( \f(CW$bitstring\fR )" 4
217 .IX Item "$ctx->add_bits( $bitstring )"
218 .PD
219 The \fBadd_bits()\fR method is an alternative to \fBadd()\fR that allow partial
220 bytes to be appended to the message. Most users can just ignore
221 this method since typical applications involve only whole-byte data.
222 .Sp
223 The two argument form of \fBadd_bits()\fR will add the first \f(CW$nbits\fR bits
224 from \f(CW$data\fR. For the last potentially partial byte only the high order
225 \&\f(CW\*(C`$nbits % 8\*(C'\fR bits are used. If \f(CW$nbits\fR is greater than \f(CW\*(C`length($data) * 8\*(C'\fR, then this method would do the same as \f(CW\*(C`$ctx\->add($data)\*(C'\fR.
226 .Sp
227 The one argument form of \fBadd_bits()\fR takes a \f(CW$bitstring\fR of "1" and "0"
228 chars as argument. It's a shorthand for \f(CW\*(C`$ctx\->add_bits(pack("B*",
229 $bitstring), length($bitstring))\*(C'\fR.
230 .Sp
231 The return value is the \f(CW$ctx\fR object itself.
232 .Sp
233 This example shows two calls that should have the same effect:
234 .Sp
235 .Vb 2
236 \& $ctx\->add_bits("111100001010");
237 \& $ctx\->add_bits("\exF0\exA0", 12);
238 .Ve
239 .Sp
240 Most digest algorithms are byte based and for these it is not possible
241 to add bits that are not a multiple of 8, and the \fBadd_bits()\fR method
242 will croak if you try.
243 .ie n .IP $ctx\->digest 4
244 .el .IP \f(CW$ctx\fR\->digest 4
245 .IX Item "$ctx->digest"
246 Return the binary digest for the message.
247 .Sp
248 Note that the \f(CW\*(C`digest\*(C'\fR operation is effectively a destructive,
249 read-once operation. Once it has been performed, the \f(CW$ctx\fR object is
250 automatically \f(CW\*(C`reset\*(C'\fR and can be used to calculate another digest
251 value. Call \f(CW$ctx\fR\->clone\->digest if you want to calculate the digest
252 without resetting the digest state.
253 .ie n .IP $ctx\->hexdigest 4
254 .el .IP \f(CW$ctx\fR\->hexdigest 4
255 .IX Item "$ctx->hexdigest"
256 Same as \f(CW$ctx\fR\->digest, but will return the digest in hexadecimal form.
257 .ie n .IP $ctx\->b64digest 4
258 .el .IP \f(CW$ctx\fR\->b64digest 4
259 .IX Item "$ctx->b64digest"
260 Same as \f(CW$ctx\fR\->digest, but will return the digest as a base64 encoded
261 string without padding.
262 .ie n .IP $ctx\->base64_padded_digest 4
263 .el .IP \f(CW$ctx\fR\->base64_padded_digest 4
264 .IX Item "$ctx->base64_padded_digest"
265 Same as \f(CW$ctx\fR\->digest, but will return the digest as a base64 encoded
266 string.
267 .SH "Digest speed"
268 .IX Header "Digest speed"
269 This table should give some indication on the relative speed of
270 different algorithms. It is sorted by throughput based on a benchmark
271 done with of some implementations of this API:
272 .PP
273 .Vb 1
274 \& Algorithm Size Implementation MB/s
275 \&
276 \& MD4 128 Digest::MD4 v1.3 165.0
277 \& MD5 128 Digest::MD5 v2.33 98.8
278 \& SHA\-256 256 Digest::SHA2 v1.1.0 66.7
279 \& SHA\-1 160 Digest::SHA v4.3.1 58.9
280 \& SHA\-1 160 Digest::SHA1 v2.10 48.8
281 \& SHA\-256 256 Digest::SHA v4.3.1 41.3
282 \& Haval\-256 256 Digest::Haval256 v1.0.4 39.8
283 \& SHA\-384 384 Digest::SHA2 v1.1.0 19.6
284 \& SHA\-512 512 Digest::SHA2 v1.1.0 19.3
285 \& SHA\-384 384 Digest::SHA v4.3.1 19.2
286 \& SHA\-512 512 Digest::SHA v4.3.1 19.2
287 \& Whirlpool 512 Digest::Whirlpool v1.0.2 13.0
288 \& MD2 128 Digest::MD2 v2.03 9.5
289 \&
290 \& Adler\-32 32 Digest::Adler32 v0.03 1.3
291 \& CRC\-16 16 Digest::CRC v0.05 1.1
292 \& CRC\-32 32 Digest::CRC v0.05 1.1
293 \& MD5 128 Digest::Perl::MD5 v1.5 1.0
294 \& CRC\-CCITT 16 Digest::CRC v0.05 0.8
295 .Ve
296 .PP
297 These numbers was achieved Apr 2004 with ActivePerl\-5.8.3 running
298 under Linux on a P4 2.8 GHz CPU. The last 5 entries differ by being
299 pure perl implementations of the algorithms, which explains why they
300 are so slow.
301 .SH "SEE ALSO"
302 .IX Header "SEE ALSO"
303 Digest::Adler32, Digest::CRC, Digest::Haval256,
304 Digest::HMAC, Digest::MD2, Digest::MD4, Digest::MD5,
305 Digest::SHA, Digest::SHA1, Digest::SHA2, Digest::Whirlpool
306 .PP
307 New digest implementations should consider subclassing from Digest::base.
308 .PP
309 MIME::Base64
310 .PP
311 http://en.wikipedia.org/wiki/Cryptographic_hash_function
312 .SH AUTHOR
313 .IX Header "AUTHOR"
314 Gisle Aas <[email protected]>
315 .PP
316 The \f(CW\*(C`Digest::\*(C'\fR interface is based on the interface originally
317 developed by Neil Winton for his \f(CW\*(C`MD5\*(C'\fR module.
318 .PP
319 This library is free software; you can redistribute it and/or
320 modify it under the same terms as Perl itself.
321 .PP
322 .Vb 2
323 \& Copyright 1998\-2006 Gisle Aas.
324 \& Copyright 1995,1996 Neil Winton.
325 .Ve