Debian Codegraph @

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