~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to Digest.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-modules-A.html">Core modules</a> &gt; <a href="index-modules-D.html">D</a> &gt; Digest</div>
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>-&gt;new<span class="s">(</span><span class="q">&quot;MD5&quot;</span><span class="s">)</span><span class="sc">;</span>
88
 
  <span class="i">$sha1</span> = <a class="l_w" href="Digest.html">Digest</a>-&gt;new<span class="s">(</span><span class="q">&quot;SHA-1&quot;</span><span class="s">)</span><span class="sc">;</span>
89
 
  <span class="i">$sha256</span> = <a class="l_w" href="Digest.html">Digest</a>-&gt;new<span class="s">(</span><span class="q">&quot;SHA-256&quot;</span><span class="s">)</span><span class="sc">;</span>
90
 
  <span class="i">$sha384</span> = <a class="l_w" href="Digest.html">Digest</a>-&gt;new<span class="s">(</span><span class="q">&quot;SHA-384&quot;</span><span class="s">)</span><span class="sc">;</span>
91
 
  <span class="i">$sha512</span> = <a class="l_w" href="Digest.html">Digest</a>-&gt;new<span class="s">(</span><span class="q">&quot;SHA-512&quot;</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>-&gt;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">-&gt;new</span><span class="s">(</span><span class="q">&quot;MD5&quot;</span><span class="s">)</span><span class="sc">;</span>
 
91
  <span class="i">$sha1</span> = <span class="w">Digest</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;SHA-1&quot;</span><span class="s">)</span><span class="sc">;</span>
 
92
  <span class="i">$sha256</span> = <span class="w">Digest</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;SHA-256&quot;</span><span class="s">)</span><span class="sc">;</span>
 
93
  <span class="i">$sha384</span> = <span class="w">Digest</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;SHA-384&quot;</span><span class="s">)</span><span class="sc">;</span>
 
94
  <span class="i">$sha512</span> = <span class="w">Digest</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;SHA-512&quot;</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">-&gt;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
109
112
digest for a different message it is wise to make it easy to plug in
110
113
stronger algorithms as the one used grow weaker.  Using the interface
111
114
documented here should make it easy to change algorithms later.</p>
112
 
<p>All <code class="inline">Digest::</code>
 
115
<p>All <code class="inline"><span class="w">Digest::</span></code>
113
116
 modules provide the same programming interface.  A
114
117
functional interface for simple use, as well as an object oriented
115
118
interface that can handle messages of arbitrary length and which can
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>
142
145
 modules:</p>
143
146
<ul>
144
147
<li><a name="%24ctx-%3d-Digest-%3eXXX(%24arg%2c...)"></a><b>$ctx = Digest-&gt;XXX($arg,...)</b>
167
170
<li><a name="%24ctx-%3ereset"></a><b>$ctx-&gt;reset</b>
168
171
<p>This is just an alias for $ctx-&gt;new.</p>
169
172
</li>
170
 
<li><a name="%24ctx-%3eadd(-%24data%2c-...-)"></a><b>$ctx-&gt;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-&gt;add( $data )</b>
 
174
</li>
 
175
<li><a name="%24ctx-%3eadd(-%24chunk1%2c-%24chunk2%2c-...-)"></a><b>$ctx-&gt;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
 
178
object itself.</p>
 
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">-&gt;add</span><span class="s">(</span><span class="q">&quot;a&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="i">$ctx</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;b&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="i">$ctx</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;c&quot;</span><span class="s">)</span><span class="sc">;</span>
 
183
  <span class="i">$ctx</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;a&quot;</span><span class="s">)</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;b&quot;</span><span class="s">)</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;c&quot;</span><span class="s">)</span><span class="sc">;</span>
 
184
  <span class="i">$ctx</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</span><span class="s">)</span><span class="sc">;</span>
 
185
  <span class="i">$ctx</span><span class="i">-&gt;add</span><span class="s">(</span><span class="q">&quot;abc&quot;</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>
173
189
</li>
174
190
<li><a name="%24ctx-%3eaddfile(-%24io_handle-)"></a><b>$ctx-&gt;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>
178
201
</li>
179
202
<li><a name="%24ctx-%3eadd_bits(-%24data%2c-%24nbits-)"></a><b>$ctx-&gt;add_bits( $data, $nbits )</b>
180
203
</li>
181
204
<li><a name="%24ctx-%3eadd_bits(-%24bitstring-)"></a><b>$ctx-&gt;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
 
208
use.</p>
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">-&gt;add</span><span class="s">(</span><span class="i">$data</span><span class="s">)</span></code>
189
 
, that is $nbits is silently ignored.</p>
 
214
.</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">-&gt;add_bits</span><span class="s">(</span><a class="l_k" href="functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;B*&quot;</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>
193
218
.</p>
 
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">-&gt;add_bits</span><span class="s">(</span><span class="q">&quot;111100001010&quot;</span><span class="s">)</span><span class="sc">;</span>
196
222
   <span class="i">$ctx</span><span class="i">-&gt;add_bits</span><span class="s">(</span><span class="q">&quot;\xF0\xA0&quot;</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>
200
226
</li>
201
227
<li><a name="%24ctx-%3edigest"></a><b>$ctx-&gt;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-&gt;clone-&gt;digest if you want to calculate the digest
208
 
without reseting the digest state.</p>
 
234
without resetting the digest state.</p>
209
235
</li>
210
236
<li><a name="%24ctx-%3ehexdigest"></a><b>$ctx-&gt;hexdigest</b>
211
237
<p>Same as $ctx-&gt;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 &lt;gisle@aas.no&gt;</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>
253
280
 module.</p>
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>
259
286
    </div>
260
287
  </div>
271
298
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
272
299
        </form>
273
300
      </p>
 
301
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
274
302
      <h2>Labels:</h2>
275
303
      <p>
276
304
        <a href="#" onClick="addLabel('Digest','Digest.html')">Add this page</a>