Edit D:\app\Administrator\product\11.2.0\dbhome_1\perl\html\lib\Digest\SHA.html
<?xml version="1.0" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Digest::SHA - Perl extension for SHA-1/224/256/384/512</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rev="made" href="mailto:" /> </head> <body style="background-color: white"> <table border="0" width="100%" cellspacing="0" cellpadding="3"> <tr><td class="block" style="background-color: #cccccc" valign="middle"> <big><strong><span class="block"> Digest::SHA - Perl extension for SHA-1/224/256/384/512</span></strong></big> </td></tr> </table> <!-- INDEX BEGIN --> <div name="index"> <p><a name="__index__"></a></p> <ul> <li><a href="#name">NAME</a></li> <li><a href="#synopsis">SYNOPSIS</a></li> <li><a href="#synopsis__hmac_sha_">SYNOPSIS (HMAC-SHA)</a></li> <li><a href="#abstract">ABSTRACT</a></li> <li><a href="#description">DESCRIPTION</a></li> <li><a href="#nist_statement_on_sha_1">NIST STATEMENT ON SHA-1</a></li> <li><a href="#padding_of_base64_digests">PADDING OF BASE64 DIGESTS</a></li> <li><a href="#export">EXPORT</a></li> <li><a href="#exportable_functions">EXPORTABLE FUNCTIONS</a></li> <li><a href="#see_also">SEE ALSO</a></li> <li><a href="#author">AUTHOR</a></li> <li><a href="#acknowledgments">ACKNOWLEDGMENTS</a></li> <li><a href="#copyright_and_license">COPYRIGHT AND LICENSE</a></li> </ul> <hr name="index" /> </div> <!-- INDEX END --> <p> </p> <h1><a name="name">NAME</a></h1> <p>Digest::SHA - Perl extension for SHA-1/224/256/384/512</p> <p> </p> <hr /> <h1><a name="synopsis">SYNOPSIS</a></h1> <p>In programs:</p> <pre> # Functional interface</pre> <pre> use Digest::SHA qw(sha1 sha1_hex sha1_base64 ...);</pre> <pre> $digest = sha1($data); $digest = sha1_hex($data); $digest = sha1_base64($data);</pre> <pre> $digest = sha256($data); $digest = sha384_hex($data); $digest = sha512_base64($data);</pre> <pre> # Object-oriented</pre> <pre> use Digest::SHA;</pre> <pre> $sha = Digest::SHA->new($alg);</pre> <pre> $sha->add($data); # feed data into stream</pre> <pre> $sha->addfile(*F); $sha->addfile($filename);</pre> <pre> $sha->add_bits($bits); $sha->add_bits($data, $nbits);</pre> <pre> $sha_copy = $sha->clone; # if needed, make copy of $sha->dump($file); # current digest state, $sha->load($file); # or save it on disk</pre> <pre> $digest = $sha->digest; # compute digest $digest = $sha->hexdigest; $digest = $sha->b64digest;</pre> <p>From the command line:</p> <pre> $ shasum files</pre> <pre> $ shasum --help</pre> <p> </p> <hr /> <h1><a name="synopsis__hmac_sha_">SYNOPSIS (HMAC-SHA)</a></h1> <pre> # Functional interface only</pre> <pre> use Digest::SHA qw(hmac_sha1 hmac_sha1_hex ...);</pre> <pre> $digest = hmac_sha1($data, $key); $digest = hmac_sha224_hex($data, $key); $digest = hmac_sha256_base64($data, $key);</pre> <p> </p> <hr /> <h1><a name="abstract">ABSTRACT</a></h1> <p>Digest::SHA is a complete implementation of the NIST Secure Hash Standard. It gives Perl programmers a convenient way to calculate SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 message digests. The module can handle all types of input, including partial-byte data.</p> <p> </p> <hr /> <h1><a name="description">DESCRIPTION</a></h1> <p>Digest::SHA is written in C for speed. If your platform lacks a C compiler, you can install the functionally equivalent (but much slower) <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/Digest/SHA/PurePerl.html">the Digest::SHA::PurePerl manpage</a> module.</p> <p>The programming interface is easy to use: it's the same one found in CPAN's <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Digest.html">the Digest manpage</a> module. So, if your applications currently use <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Digest/MD5.html">the Digest::MD5 manpage</a> and you'd prefer the stronger security of SHA, it's a simple matter to convert them.</p> <p>The interface provides two ways to calculate digests: all-at-once, or in stages. To illustrate, the following short program computes the SHA-256 digest of "hello world" using each approach:</p> <pre> use Digest::SHA qw(sha256_hex);</pre> <pre> $data = "hello world"; @frags = split(//, $data);</pre> <pre> # all-at-once (Functional style) $digest1 = sha256_hex($data);</pre> <pre> # in-stages (OOP style) $state = Digest::SHA->new(256); for (@frags) { $state->add($_) } $digest2 = $state->hexdigest;</pre> <pre> print $digest1 eq $digest2 ? "whew!\n" : "oops!\n";</pre> <p>To calculate the digest of an n-bit message where <em>n</em> is not a multiple of 8, use the <em>add_bits()</em> method. For example, consider the 446-bit message consisting of the bit-string "110" repeated 148 times, followed by "11". Here's how to display its SHA-1 digest:</p> <pre> use Digest::SHA; $bits = "110" x 148 . "11"; $sha = Digest::SHA->new(1)->add_bits($bits); print $sha->hexdigest, "\n";</pre> <p>Note that for larger bit-strings, it's more efficient to use the two-argument version <em>add_bits($data, $nbits)</em>, where <em>$data</em> is in the customary packed binary format used for Perl strings.</p> <p>The module also lets you save intermediate SHA states to disk, or display them on standard output. The <em>dump()</em> method generates portable, human-readable text describing the current state of computation. You can subsequently retrieve the file with <em>load()</em> to resume where the calculation left off.</p> <p>To see what a state description looks like, just run the following:</p> <pre> use Digest::SHA; Digest::SHA->new->add("Shaw" x 1962)->dump;</pre> <p>As an added convenience, the Digest::SHA module offers routines to calculate keyed hashes using the HMAC-SHA-1/224/256/384/512 algorithms. These services exist in functional form only, and mimic the style and behavior of the <em>sha()</em>, <em>sha_hex()</em>, and <em>sha_base64()</em> functions.</p> <pre> # Test vector from draft-ietf-ipsec-ciph-sha-256-01.txt</pre> <pre> use Digest::SHA qw(hmac_sha256_hex); print hmac_sha256_hex("Hi There", chr(0x0b) x 32), "\n";</pre> <p> </p> <hr /> <h1><a name="nist_statement_on_sha_1">NIST STATEMENT ON SHA-1</a></h1> <p><em>NIST was recently informed that researchers had discovered a way to "break" the current Federal Information Processing Standard SHA-1 algorithm, which has been in effect since 1994. The researchers have not yet published their complete results, so NIST has not confirmed these findings. However, the researchers are a reputable research team with expertise in this area.</em></p> <p><em>Due to advances in computing power, NIST already planned to phase out SHA-1 in favor of the larger and stronger hash functions (SHA-224, SHA-256, SHA-384 and SHA-512) by 2010. New developments should use the larger and stronger hash functions.</em></p> <p>ref. <a href="http://www.csrc.nist.gov/pki/HashWorkshop/NIST%20Statement/Burr_Mar2005.html">http://www.csrc.nist.gov/pki/HashWorkshop/NIST%20Statement/Burr_Mar2005.html</a></p> <p> </p> <hr /> <h1><a name="padding_of_base64_digests">PADDING OF BASE64 DIGESTS</a></h1> <p>By convention, CPAN Digest modules do <strong>not</strong> pad their Base64 output. Problems can occur when feeding such digests to other software that expects properly padded Base64 encodings.</p> <p>For the time being, any necessary padding must be done by the user. Fortunately, this is a simple operation: if the length of a Base64-encoded digest isn't a multiple of 4, simply append "=" characters to the end of the digest until it is:</p> <pre> while (length($b64_digest) % 4) { $b64_digest .= '='; }</pre> <p>To illustrate, <em>sha256_base64("abc")</em> is computed to be</p> <pre> ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0</pre> <p>which has a length of 43. So, the properly padded version is</p> <pre> ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=</pre> <p> </p> <hr /> <h1><a name="export">EXPORT</a></h1> <p>None by default.</p> <p> </p> <hr /> <h1><a name="exportable_functions">EXPORTABLE FUNCTIONS</a></h1> <p>Provided your C compiler supports a 64-bit type (e.g. the <em>long long</em> of C99, or <em>__int64</em> used by Microsoft C/C++), all of these functions will be available for use. Otherwise, you won't be able to perform the SHA-384 and SHA-512 transforms, both of which require 64-bit operations.</p> <p><em>Functional style</em></p> <dl> <dt><strong><a name="sha1" class="item"><strong>sha1($data, ...)</strong></a></strong> <dt><strong><a name="sha224" class="item"><strong>sha224($data, ...)</strong></a></strong> <dt><strong><a name="sha256" class="item"><strong>sha256($data, ...)</strong></a></strong> <dt><strong><a name="sha384" class="item"><strong>sha384($data, ...)</strong></a></strong> <dt><strong><a name="sha512" class="item"><strong>sha512($data, ...)</strong></a></strong> <dd> <p>Logically joins the arguments into a single string, and returns its SHA-1/224/256/384/512 digest encoded as a binary string.</p> </dd> </li> <dt><strong><a name="sha1_hex" class="item"><strong>sha1_hex($data, ...)</strong></a></strong> <dt><strong><a name="sha224_hex" class="item"><strong>sha224_hex($data, ...)</strong></a></strong> <dt><strong><a name="sha256_hex" class="item"><strong>sha256_hex($data, ...)</strong></a></strong> <dt><strong><a name="sha384_hex" class="item"><strong>sha384_hex($data, ...)</strong></a></strong> <dt><strong><a name="sha512_hex" class="item"><strong>sha512_hex($data, ...)</strong></a></strong> <dd> <p>Logically joins the arguments into a single string, and returns its SHA-1/224/256/384/512 digest encoded as a hexadecimal string.</p> </dd> </li> <dt><strong><a name="sha1_base64" class="item"><strong>sha1_base64($data, ...)</strong></a></strong> <dt><strong><a name="sha224_base64" class="item"><strong>sha224_base64($data, ...)</strong></a></strong> <dt><strong><a name="sha256_base64" class="item"><strong>sha256_base64($data, ...)</strong></a></strong> <dt><strong><a name="sha384_base64" class="item"><strong>sha384_base64($data, ...)</strong></a></strong> <dt><strong><a name="sha512_base64" class="item"><strong>sha512_base64($data, ...)</strong></a></strong> <dd> <p>Logically joins the arguments into a single string, and returns its SHA-1/224/256/384/512 digest encoded as a Base64 string.</p> </dd> <dd> <p>It's important to note that the resulting string does <strong>not</strong> contain the padding characters typical of Base64 encodings. This omission is deliberate, and is done to maintain compatibility with the family of CPAN Digest modules. See <a href="#padding_of_base64_digests">PADDING OF BASE64 DIGESTS</a> for details.</p> </dd> </li> </dl> <p><em>OOP style</em></p> <dl> <dt><strong><a name="new" class="item"><strong>new($alg)</strong></a></strong> <dd> <p>Returns a new Digest::SHA object. Allowed values for <em>$alg</em> are 1, 224, 256, 384, or 512. It's also possible to use common string representations of the algorithm (e.g. "sha256", "SHA-384"). If the argument is missing, SHA-1 will be used by default.</p> </dd> <dd> <p>Invoking <em>new</em> as an instance method will not create a new object; instead, it will simply reset the object to the initial state associated with <em>$alg</em>. If the argument is missing, the object will continue using the same algorithm that was selected at creation.</p> </dd> </li> <dt><strong><a name="reset" class="item"><strong>reset($alg)</strong></a></strong> <dd> <p>This method has exactly the same effect as <em>new($alg)</em>. In fact, <em>reset</em> is just an alias for <em>new</em>.</p> </dd> </li> <dt><strong><a name="hashsize" class="item"><strong>hashsize</strong></a></strong> <dd> <p>Returns the number of digest bits for this object. The values are 160, 224, 256, 384, and 512 for SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512, respectively.</p> </dd> </li> <dt><strong><a name="algorithm" class="item"><strong>algorithm</strong></a></strong> <dd> <p>Returns the digest algorithm for this object. The values are 1, 224, 256, 384, and 512 for SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512, respectively.</p> </dd> </li> <dt><strong><a name="clone" class="item"><strong>clone</strong></a></strong> <dd> <p>Returns a duplicate copy of the object.</p> </dd> </li> <dt><strong><a name="add" class="item"><strong>add($data, ...)</strong></a></strong> <dd> <p>Logically joins the arguments into a single string, and uses it to update the current digest state. In other words, the following statements have the same effect:</p> </dd> <dd> <pre> $sha->add("a"); $sha->add("b"); $sha->add("c"); $sha->add("a")->add("b")->add("c"); $sha->add("a", "b", "c"); $sha->add("abc");</pre> </dd> <dd> <p>The return value is the updated object itself.</p> </dd> </li> <dt><strong><a name="add_bits" class="item"><strong>add_bits($data, $nbits)</strong></a></strong> <dt><strong><strong>add_bits($bits)</strong></strong> <dd> <p>Updates the current digest state by appending bits to it. The return value is the updated object itself.</p> </dd> <dd> <p>The first form causes the most-significant <em>$nbits</em> of <em>$data</em> to be appended to the stream. The <em>$data</em> argument is in the customary binary format used for Perl strings.</p> </dd> <dd> <p>The second form takes an ASCII string of "0" and "1" characters as its argument. It's equivalent to</p> </dd> <dd> <pre> $sha->add_bits(pack("B*", $bits), length($bits));</pre> </dd> <dd> <p>So, the following two statements do the same thing:</p> </dd> <dd> <pre> $sha->add_bits("111100001010"); $sha->add_bits("\xF0\xA0", 12);</pre> </dd> </li> <dt><strong><a name="addfile" class="item"><strong>addfile(*FILE)</strong></a></strong> <dd> <p>Reads from <em>FILE</em> until EOF, and appends that data to the current state. The return value is the updated object itself.</p> </dd> </li> <dt><strong><strong>addfile($filename [, $mode])</strong></strong> <dd> <p>Reads the contents of <em>$filename</em>, and appends that data to the current state. The return value is the updated object itself.</p> </dd> <dd> <p>By default, <em>$filename</em> is simply opened and read; no special modes or I/O disciplines are used. To change this, set the optional <em>$mode</em> argument to one of the following values:</p> </dd> <dd> <pre> "b" read file in binary mode</pre> </dd> <dd> <pre> "p" use portable mode</pre> </dd> <dd> <p>The "p" mode is handy since it ensures that the digest value of <em>$filename</em> will be the same when computed on different operating systems. It accomplishes this by internally translating all newlines in text files to UNIX format before calculating the digest; on the other hand, binary files are read in raw mode with no translation whatsoever.</p> </dd> <dd> <p>For a fuller discussion of newline formats, refer to CPAN module <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/File/LocalizeNewlines.html">the File::LocalizeNewlines manpage</a>. Its "universal line separator" regex forms the basis of <em>addfile</em>'s portable mode processing.</p> </dd> </li> <dt><strong><a name="dump" class="item"><strong>dump($filename)</strong></a></strong> <dd> <p>Provides persistent storage of intermediate SHA states by writing a portable, human-readable representation of the current state to <em>$filename</em>. If the argument is missing, or equal to the empty string, the state information will be written to STDOUT.</p> </dd> </li> <dt><strong><a name="load" class="item"><strong>load($filename)</strong></a></strong> <dd> <p>Returns a Digest::SHA object representing the intermediate SHA state that was previously dumped to <em>$filename</em>. If called as a class method, a new object is created; if called as an instance method, the object is reset to the state contained in <em>$filename</em>. If the argument is missing, or equal to the empty string, the state information will be read from STDIN.</p> </dd> </li> <dt><strong><a name="digest" class="item"><strong>digest</strong></a></strong> <dd> <p>Returns the digest encoded as a binary string.</p> </dd> <dd> <p>Note that the <em>digest</em> method is a read-once operation. Once it has been performed, the Digest::SHA object is automatically reset in preparation for calculating another digest value. Call <em>$sha->clone->digest</em> if it's necessary to preserve the original digest state.</p> </dd> </li> <dt><strong><a name="hexdigest" class="item"><strong>hexdigest</strong></a></strong> <dd> <p>Returns the digest encoded as a hexadecimal string.</p> </dd> <dd> <p>Like <em>digest</em>, this method is a read-once operation. Call <em>$sha->clone->hexdigest</em> if it's necessary to preserve the original digest state.</p> </dd> <dd> <p>This method is inherited if <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Digest/base.html">the Digest::base manpage</a> is installed on your system. Otherwise, a functionally equivalent substitute is used.</p> </dd> </li> <dt><strong><a name="b64digest" class="item"><strong>b64digest</strong></a></strong> <dd> <p>Returns the digest encoded as a Base64 string.</p> </dd> <dd> <p>Like <em>digest</em>, this method is a read-once operation. Call <em>$sha->clone->b64digest</em> if it's necessary to preserve the original digest state.</p> </dd> <dd> <p>This method is inherited if <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Digest/base.html">the Digest::base manpage</a> is installed on your system. Otherwise, a functionally equivalent substitute is used.</p> </dd> <dd> <p>It's important to note that the resulting string does <strong>not</strong> contain the padding characters typical of Base64 encodings. This omission is deliberate, and is done to maintain compatibility with the family of CPAN Digest modules. See <a href="#padding_of_base64_digests">PADDING OF BASE64 DIGESTS</a> for details.</p> </dd> </li> </dl> <p><em>HMAC-SHA-1/224/256/384/512</em></p> <dl> <dt><strong><a name="hmac_sha1" class="item"><strong>hmac_sha1($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha224" class="item"><strong>hmac_sha224($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha256" class="item"><strong>hmac_sha256($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha384" class="item"><strong>hmac_sha384($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha512" class="item"><strong>hmac_sha512($data, $key)</strong></a></strong> <dd> <p>Returns the HMAC-SHA-1/224/256/384/512 digest of <em>$data</em>/<em>$key</em>, with the result encoded as a binary string. Multiple <em>$data</em> arguments are allowed, provided that <em>$key</em> is the last argument in the list.</p> </dd> </li> <dt><strong><a name="hmac_sha1_hex" class="item"><strong>hmac_sha1_hex($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha224_hex" class="item"><strong>hmac_sha224_hex($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha256_hex" class="item"><strong>hmac_sha256_hex($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha384_hex" class="item"><strong>hmac_sha384_hex($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha512_hex" class="item"><strong>hmac_sha512_hex($data, $key)</strong></a></strong> <dd> <p>Returns the HMAC-SHA-1/224/256/384/512 digest of <em>$data</em>/<em>$key</em>, with the result encoded as a hexadecimal string. Multiple <em>$data</em> arguments are allowed, provided that <em>$key</em> is the last argument in the list.</p> </dd> </li> <dt><strong><a name="hmac_sha1_base64" class="item"><strong>hmac_sha1_base64($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha224_base64" class="item"><strong>hmac_sha224_base64($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha256_base64" class="item"><strong>hmac_sha256_base64($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha384_base64" class="item"><strong>hmac_sha384_base64($data, $key)</strong></a></strong> <dt><strong><a name="hmac_sha512_base64" class="item"><strong>hmac_sha512_base64($data, $key)</strong></a></strong> <dd> <p>Returns the HMAC-SHA-1/224/256/384/512 digest of <em>$data</em>/<em>$key</em>, with the result encoded as a Base64 string. Multiple <em>$data</em> arguments are allowed, provided that <em>$key</em> is the last argument in the list.</p> </dd> <dd> <p>It's important to note that the resulting string does <strong>not</strong> contain the padding characters typical of Base64 encodings. This omission is deliberate, and is done to maintain compatibility with the family of CPAN Digest modules. See <a href="#padding_of_base64_digests">PADDING OF BASE64 DIGESTS</a> for details.</p> </dd> </li> </dl> <p> </p> <hr /> <h1><a name="see_also">SEE ALSO</a></h1> <p><a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Digest.html">the Digest manpage</a>, <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/Digest/SHA/PurePerl.html">the Digest::SHA::PurePerl manpage</a></p> <p>The Secure Hash Standard (FIPS PUB 180-2) can be found at:</p> <p><a href="http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf">http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf</a></p> <p>The Keyed-Hash Message Authentication Code (HMAC):</p> <p><a href="http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf">http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf</a></p> <p> </p> <hr /> <h1><a name="author">AUTHOR</a></h1> <pre> Mark Shelor <mshelor@cpan.org></pre> <p> </p> <hr /> <h1><a name="acknowledgments">ACKNOWLEDGMENTS</a></h1> <p>The author is particularly grateful to</p> <pre> Gisle Aas Chris Carey Jim Doble Julius Duque Jeffrey Friedl Robert Gilmour Brian Gladman Adam Kennedy Andy Lester Alex Muntada Steve Peters Chris Skiscim Martin Thurn Gunnar Wolf Adam Woodbury</pre> <p>for their valuable comments and suggestions.</p> <p> </p> <hr /> <h1><a name="copyright_and_license">COPYRIGHT AND LICENSE</a></h1> <p>Copyright (C) 2003-2007 Mark Shelor</p> <p>This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> <p><a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlartistic.html">the perlartistic manpage</a></p> <table border="0" width="100%" cellspacing="0" cellpadding="3"> <tr><td class="block" style="background-color: #cccccc" valign="middle"> <big><strong><span class="block"> Digest::SHA - Perl extension for SHA-1/224/256/384/512</span></strong></big> </td></tr> </table> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de