84
87
<div id="contentBody"><div class="title_container"><div class="page_title">Digest</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OO-INTERFACE">OO INTERFACE</a><li><a href="#Digest-speed">Digest speed</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME</h1>
85
88
<p>Digest - Modules that calculate message digests</p>
86
89
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
87
<pre class="verbatim"> <span class="i">$md5</span> = <a class="l_w" href="Digest.html">Digest</a>->new<span class="s">(</span><span class="q">"MD5"</span><span class="s">)</span><span class="sc">;</span>
88
<span class="i">$sha1</span> = <a class="l_w" href="Digest.html">Digest</a>->new<span class="s">(</span><span class="q">"SHA-1"</span><span class="s">)</span><span class="sc">;</span>
89
<span class="i">$sha256</span> = <a class="l_w" href="Digest.html">Digest</a>->new<span class="s">(</span><span class="q">"SHA-256"</span><span class="s">)</span><span class="sc">;</span>
90
<span class="i">$sha384</span> = <a class="l_w" href="Digest.html">Digest</a>->new<span class="s">(</span><span class="q">"SHA-384"</span><span class="s">)</span><span class="sc">;</span>
91
<span class="i">$sha512</span> = <a class="l_w" href="Digest.html">Digest</a>->new<span class="s">(</span><span class="q">"SHA-512"</span><span class="s">)</span><span class="sc">;</span></pre>
92
<pre class="verbatim"> <span class="i">$hmac</span> = <a class="l_w" href="Digest.html">Digest</a>->HMAC_MD5<span class="s">(</span><span class="i">$key</span><span class="s">)</span><span class="sc">;</span></pre>
90
<pre class="verbatim"> <span class="i">$md5</span> = <span class="w">Digest</span><span class="w">->new</span><span class="s">(</span><span class="q">"MD5"</span><span class="s">)</span><span class="sc">;</span>
91
<span class="i">$sha1</span> = <span class="w">Digest</span><span class="w">->new</span><span class="s">(</span><span class="q">"SHA-1"</span><span class="s">)</span><span class="sc">;</span>
92
<span class="i">$sha256</span> = <span class="w">Digest</span><span class="w">->new</span><span class="s">(</span><span class="q">"SHA-256"</span><span class="s">)</span><span class="sc">;</span>
93
<span class="i">$sha384</span> = <span class="w">Digest</span><span class="w">->new</span><span class="s">(</span><span class="q">"SHA-384"</span><span class="s">)</span><span class="sc">;</span>
94
<span class="i">$sha512</span> = <span class="w">Digest</span><span class="w">->new</span><span class="s">(</span><span class="q">"SHA-512"</span><span class="s">)</span><span class="sc">;</span></pre>
95
<pre class="verbatim"> <span class="i">$hmac</span> = <span class="w">Digest</span><span class="w">->HMAC_MD5</span><span class="s">(</span><span class="i">$key</span><span class="s">)</span><span class="sc">;</span></pre>
93
96
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
94
<p>The <code class="inline">Digest::</code>
97
<p>The <code class="inline"><span class="w">Digest::</span></code>
95
98
modules calculate digests, also called "fingerprints"
96
99
or "hashes", of some data, called a message. The digest is (usually)
97
100
some small/fixed size string. The actual size of the digest depend of
133
136
<p>The functional interface is simply importable functions with the same
134
137
name as the algorithm. The functions take the message as argument and
135
138
return the digest. Example:</p>
136
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="Digest/MD5.html">Digest::MD5</a> <span class="q">qw(md5)</span><span class="sc">;</span>
139
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">Digest::MD5</span> <span class="q">qw(md5)</span><span class="sc">;</span>
137
140
<span class="i">$digest</span> = <span class="i">md5</span><span class="s">(</span><span class="i">$message</span><span class="s">)</span><span class="sc">;</span></pre>
138
141
<p>There are also versions of the functions with "_hex" or "_base64"
139
142
appended to the name, which returns the digest in the indicated form.</p>
140
143
<a name="OO-INTERFACE"></a><h1>OO INTERFACE</h1>
141
<p>The following methods are available for all <code class="inline">Digest::</code>
144
<p>The following methods are available for all <code class="inline"><span class="w">Digest::</span></code>
144
147
<li><a name="%24ctx-%3d-Digest-%3eXXX(%24arg%2c...)"></a><b>$ctx = Digest->XXX($arg,...)</b>
167
170
<li><a name="%24ctx-%3ereset"></a><b>$ctx->reset</b>
168
171
<p>This is just an alias for $ctx->new.</p>
170
<li><a name="%24ctx-%3eadd(-%24data%2c-...-)"></a><b>$ctx->add( $data, ... )</b>
171
<p>The $data provided as argument are appended to the message we
172
calculate the digest for. The return value is the $ctx object itself.</p>
173
<li><a name="%24ctx-%3eadd(-%24data-)"></a><b>$ctx->add( $data )</b>
175
<li><a name="%24ctx-%3eadd(-%24chunk1%2c-%24chunk2%2c-...-)"></a><b>$ctx->add( $chunk1, $chunk2, ... )</b>
176
<p>The string value of the $data provided as argument is appended to the
177
message we calculate the digest for. The return value is the $ctx
179
<p>If more arguments are provided then they are all appended to the
180
message, thus all these lines will have the same effect on the state
181
of the $ctx object:</p>
182
<pre class="verbatim"> <span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"a"</span><span class="s">)</span><span class="sc">;</span> <span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"b"</span><span class="s">)</span><span class="sc">;</span> <span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"c"</span><span class="s">)</span><span class="sc">;</span>
183
<span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"a"</span><span class="s">)</span><span class="i">->add</span><span class="s">(</span><span class="q">"b"</span><span class="s">)</span><span class="i">->add</span><span class="s">(</span><span class="q">"c"</span><span class="s">)</span><span class="sc">;</span>
184
<span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"a"</span><span class="cm">,</span> <span class="q">"b"</span><span class="cm">,</span> <span class="q">"c"</span><span class="s">)</span><span class="sc">;</span>
185
<span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="q">"abc"</span><span class="s">)</span><span class="sc">;</span></pre>
186
<p>Most algorithms are only defined for strings of bytes and this method
187
might therefore croak if the provided arguments contain chars with
188
ordinal number above 255.</p>
174
190
<li><a name="%24ctx-%3eaddfile(-%24io_handle-)"></a><b>$ctx->addfile( $io_handle )</b>
175
191
<p>The $io_handle is read until EOF and the content is appended to the
176
192
message we calculate the digest for. The return value is the $ctx
177
193
object itself.</p>
194
<p>The addfile() method will croak() if it fails reading data for some
195
reason. If it croaks it is unpredictable what the state of the $ctx
196
object will be in. The addfile() method might have been able to read
197
the file partially before it failed. It is probably wise to discard
198
or reset the $ctx object if this occurs.</p>
199
<p>In most cases you want to make sure that the $io_handle is in
200
"binmode" before you pass it as argument to the addfile() method.</p>
179
202
<li><a name="%24ctx-%3eadd_bits(-%24data%2c-%24nbits-)"></a><b>$ctx->add_bits( $data, $nbits )</b>
181
204
<li><a name="%24ctx-%3eadd_bits(-%24bitstring-)"></a><b>$ctx->add_bits( $bitstring )</b>
182
<p>The bits provided are appended to the message we calculate the digest
183
for. The return value is the $ctx object itself.</p>
205
<p>The add_bits() method is an alternative to add() that allow partial
206
bytes to be appended to the message. Most users should just ignore
207
this method as partial bytes is very unlikely to be of any practical
184
209
<p>The two argument form of add_bits() will add the first $nbits bits
185
from data. For the last potentially partial byte only the high order
210
from $data. For the last potentially partial byte only the high order
186
211
<code class="inline"><span class="i">$nbits</span> % <span class="n">8</span></code>
187
212
bits are used. If $nbits is greater than <code class="inline"><a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$data</span><span class="s">)</span> * <span class="n">8</span></code>
188
213
, then this method would do the same as <code class="inline"><span class="i">$ctx</span><span class="i">->add</span><span class="s">(</span><span class="i">$data</span><span class="s">)</span></code>
189
, that is $nbits is silently ignored.</p>
190
215
<p>The one argument form of add_bits() takes a $bitstring of "1" and "0"
191
216
chars as argument. It's a shorthand for <code class="inline"><span class="i">$ctx</span><span class="i">->add_bits</span><span class="s">(</span><a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">"B*"</span><span class="cm">,</span>
192
217
<span class="i">$bitstring</span><span class="s">)</span><span class="cm">,</span> <a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$bitstring</span><span class="s">)</span><span class="s">)</span></code>
219
<p>The return value is the $ctx object itself.</p>
194
220
<p>This example shows two calls that should have the same effect:</p>
195
221
<pre class="verbatim"> <span class="i">$ctx</span><span class="i">->add_bits</span><span class="s">(</span><span class="q">"111100001010"</span><span class="s">)</span><span class="sc">;</span>
196
222
<span class="i">$ctx</span><span class="i">->add_bits</span><span class="s">(</span><span class="q">"\xF0\xA0"</span><span class="cm">,</span> <span class="n">12</span><span class="s">)</span><span class="sc">;</span></pre>
197
<p>Most digest algorithms are byte based. For those it is not possible
223
<p>Most digest algorithms are byte based and for these it is not possible
198
224
to add bits that are not a multiple of 8, and the add_bits() method
199
225
will croak if you try.</p>
201
227
<li><a name="%24ctx-%3edigest"></a><b>$ctx->digest</b>
202
228
<p>Return the binary digest for the message.</p>
203
<p>Note that the <code class="inline">digest</code>
229
<p>Note that the <code class="inline"><span class="w">digest</span></code>
204
230
operation is effectively a destructive,
205
231
read-once operation. Once it has been performed, the $ctx object is
206
232
automatically <code class="inline"><a class="l_k" href="functions/reset.html">reset</a></code> and can be used to calculate another digest
207
233
value. Call $ctx->clone->digest if you want to calculate the digest
208
without reseting the digest state.</p>
234
without resetting the digest state.</p>
210
236
<li><a name="%24ctx-%3ehexdigest"></a><b>$ctx->hexdigest</b>
211
237
<p>Same as $ctx->digest, but will return the digest in hexadecimal form.</p>
242
268
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
243
269
<p><a href="http://search.cpan.org/perldoc/Digest::Adler32">Digest::Adler32</a>, <a href="http://search.cpan.org/perldoc/Digest::CRC">Digest::CRC</a>, <a href="http://search.cpan.org/perldoc/Digest::Haval256">Digest::Haval256</a>,
244
270
<a href="http://search.cpan.org/perldoc/Digest::HMAC">Digest::HMAC</a>, <a href="http://search.cpan.org/perldoc/Digest::MD2">Digest::MD2</a>, <a href="http://search.cpan.org/perldoc/Digest::MD4">Digest::MD4</a>, <a href="Digest/MD5.html">Digest::MD5</a>,
245
<a href="http://search.cpan.org/perldoc/Digest::SHA">Digest::SHA</a>, <a href="http://search.cpan.org/perldoc/Digest::SHA1">Digest::SHA1</a>, <a href="http://search.cpan.org/perldoc/Digest::SHA2">Digest::SHA2</a>, <a href="http://search.cpan.org/perldoc/Digest::Whirlpool">Digest::Whirlpool</a></p>
271
<a href="Digest/SHA.html">Digest::SHA</a>, <a href="http://search.cpan.org/perldoc/Digest::SHA1">Digest::SHA1</a>, <a href="http://search.cpan.org/perldoc/Digest::SHA2">Digest::SHA2</a>, <a href="http://search.cpan.org/perldoc/Digest::Whirlpool">Digest::Whirlpool</a></p>
246
272
<p>New digest implementations should consider subclassing from <a href="Digest/base.html">Digest::base</a>.</p>
247
273
<p><a href="MIME/Base64.html">MIME::Base64</a></p>
274
<p><a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">http://en.wikipedia.org/wiki/Cryptographic_hash_function</a></p>
248
275
<a name="AUTHOR"></a><h1>AUTHOR</h1>
249
276
<p>Gisle Aas <gisle@aas.no></p>
250
<p>The <code class="inline">Digest::</code>
277
<p>The <code class="inline"><span class="w">Digest::</span></code>
251
278
interface is based on the interface originally
252
developed by Neil Winton for his <code class="inline">MD5</code>
279
developed by Neil Winton for his <code class="inline"><span class="w">MD5</span></code>
254
281
<p>This library is free software; you can redistribute it and/or
255
282
modify it under the same terms as Perl itself.</p>
256
<pre class="verbatim"> Copyright 1998-2001,2003-2004 Gisle Aas.
257
Copyright 1995-1996 Neil Winton.</pre></div>
283
<pre class="verbatim"> Copyright 1998-2006 Gisle Aas.
284
Copyright 1995,1996 Neil Winton.</pre></div>
258
285
<div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>