📄 sha.pm
字号:
while (length($b64_digest) % 4) { $b64_digest .= '='; }To illustrate, I<sha256_base64("abc")> is computed to be ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0which has a length of 43. So, the properly padded version is ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0==head1 EXPORTNone by default.=head1 EXPORTABLE FUNCTIONSProvided your C compiler supports a 64-bit type (e.g. the I<longlong> of C99, or I<__int64> used by Microsoft C/C++), all of thesefunctions will be available for use. Otherwise, you won't be ableto perform the SHA-384 and SHA-512 transforms, both of which require64-bit operations.I<Functional style>=over 4=item B<sha1($data, ...)>=item B<sha224($data, ...)>=item B<sha256($data, ...)>=item B<sha384($data, ...)>=item B<sha512($data, ...)>Logically joins the arguments into a single string, and returnsits SHA-1/224/256/384/512 digest encoded as a binary string.=item B<sha1_hex($data, ...)>=item B<sha224_hex($data, ...)>=item B<sha256_hex($data, ...)>=item B<sha384_hex($data, ...)>=item B<sha512_hex($data, ...)>Logically joins the arguments into a single string, and returnsits SHA-1/224/256/384/512 digest encoded as a hexadecimal string.=item B<sha1_base64($data, ...)>=item B<sha224_base64($data, ...)>=item B<sha256_base64($data, ...)>=item B<sha384_base64($data, ...)>=item B<sha512_base64($data, ...)>Logically joins the arguments into a single string, and returnsits SHA-1/224/256/384/512 digest encoded as a Base64 string.It's important to note that the resulting string does B<not> containthe padding characters typical of Base64 encodings. This omission isdeliberate, and is done to maintain compatibility with the family ofCPAN Digest modules. See L</"PADDING OF BASE64 DIGESTS"> for details.=backI<OOP style>=over 4=item B<new($alg)>Returns a new Digest::SHA object. Allowed values for I<$alg> are1, 224, 256, 384, or 512. It's also possible to use common stringrepresentations of the algorithm (e.g. "sha256", "SHA-384"). Ifthe argument is missing, SHA-1 will be used by default.Invoking I<new> as an instance method will not create a new object;instead, it will simply reset the object to the initial stateassociated with I<$alg>. If the argument is missing, the objectwill continue using the same algorithm that was selected at creation.=item B<reset($alg)>This method has exactly the same effect as I<new($alg)>. In fact,I<reset> is just an alias for I<new>.=item B<hashsize>Returns the number of digest bits for this object. The values are160, 224, 256, 384, and 512 for SHA-1, SHA-224, SHA-256, SHA-384,and SHA-512, respectively.=item B<algorithm>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, andSHA-512, respectively.=item B<clone>Returns a duplicate copy of the object.=item B<add($data, ...)>Logically joins the arguments into a single string, and uses it toupdate the current digest state. In other words, the followingstatements have the same effect: $sha->add("a"); $sha->add("b"); $sha->add("c"); $sha->add("a")->add("b")->add("c"); $sha->add("a", "b", "c"); $sha->add("abc");The return value is the updated object itself.=item B<add_bits($data, $nbits)>=item B<add_bits($bits)>Updates the current digest state by appending bits to it. Thereturn value is the updated object itself.The first form causes the most-significant I<$nbits> of I<$data>to be appended to the stream. The I<$data> argument is in thecustomary binary format used for Perl strings.The second form takes an ASCII string of "0" and "1" characters asits argument. It's equivalent to $sha->add_bits(pack("B*", $bits), length($bits));So, the following two statements do the same thing: $sha->add_bits("111100001010"); $sha->add_bits("\xF0\xA0", 12);=item B<addfile(*FILE)>Reads from I<FILE> until EOF, and appends that data to the currentstate. The return value is the updated object itself.=item B<addfile($filename [, $mode])>Reads the contents of I<$filename>, and appends that data to the currentstate. The return value is the updated object itself.By default, I<$filename> is simply opened and read; no special modesor I/O disciplines are used. To change this, set the optional I<$mode>argument to one of the following values: "b" read file in binary mode "p" use portable modeThe "p" mode is handy since it ensures that the digest value ofI<$filename> will be the same when computed on different operatingsystems. It accomplishes this by internally translating all newlinesin text files to UNIX format before calculating the digest; on the otherhand, binary files are read in raw mode with no translation whatsoever.For a fuller discussion of newline formats, refer to CPAN moduleL<File::LocalizeNewlines>. Its "universal line separator" regex formsthe basis of I<addfile>'s portable mode processing.=item B<dump($filename)>Provides persistent storage of intermediate SHA states by writinga portable, human-readable representation of the current state toI<$filename>. If the argument is missing, or equal to the emptystring, the state information will be written to STDOUT.=item B<load($filename)>Returns a Digest::SHA object representing the intermediate SHAstate that was previously dumped to I<$filename>. If called as aclass method, a new object is created; if called as an instancemethod, the object is reset to the state contained in I<$filename>.If the argument is missing, or equal to the empty string, the stateinformation will be read from STDIN.=item B<digest>Returns the digest encoded as a binary string.Note that the I<digest> method is a read-once operation. Once ithas been performed, the Digest::SHA object is automatically resetin preparation for calculating another digest value. CallI<$sha-E<gt>clone-E<gt>digest> if it's necessary to preserve theoriginal digest state.=item B<hexdigest>Returns the digest encoded as a hexadecimal string.Like I<digest>, this method is a read-once operation. CallI<$sha-E<gt>clone-E<gt>hexdigest> if it's necessary to preservethe original digest state.This method is inherited if L<Digest::base> is installed on yoursystem. Otherwise, a functionally equivalent substitute is used.=item B<b64digest>Returns the digest encoded as a Base64 string.Like I<digest>, this method is a read-once operation. CallI<$sha-E<gt>clone-E<gt>b64digest> if it's necessary to preservethe original digest state.This method is inherited if L<Digest::base> is installed on yoursystem. Otherwise, a functionally equivalent substitute is used.It's important to note that the resulting string does B<not> containthe padding characters typical of Base64 encodings. This omission isdeliberate, and is done to maintain compatibility with the family ofCPAN Digest modules. See L</"PADDING OF BASE64 DIGESTS"> for details.=backI<HMAC-SHA-1/224/256/384/512>=over 4=item B<hmac_sha1($data, $key)>=item B<hmac_sha224($data, $key)>=item B<hmac_sha256($data, $key)>=item B<hmac_sha384($data, $key)>=item B<hmac_sha512($data, $key)>Returns the HMAC-SHA-1/224/256/384/512 digest of I<$data>/I<$key>,with the result encoded as a binary string. Multiple I<$data>arguments are allowed, provided that I<$key> is the last argumentin the list.=item B<hmac_sha1_hex($data, $key)>=item B<hmac_sha224_hex($data, $key)>=item B<hmac_sha256_hex($data, $key)>=item B<hmac_sha384_hex($data, $key)>=item B<hmac_sha512_hex($data, $key)>Returns the HMAC-SHA-1/224/256/384/512 digest of I<$data>/I<$key>,with the result encoded as a hexadecimal string. Multiple I<$data>arguments are allowed, provided that I<$key> is the last argumentin the list.=item B<hmac_sha1_base64($data, $key)>=item B<hmac_sha224_base64($data, $key)>=item B<hmac_sha256_base64($data, $key)>=item B<hmac_sha384_base64($data, $key)>=item B<hmac_sha512_base64($data, $key)>Returns the HMAC-SHA-1/224/256/384/512 digest of I<$data>/I<$key>,with the result encoded as a Base64 string. Multiple I<$data>arguments are allowed, provided that I<$key> is the last argumentin the list.It's important to note that the resulting string does B<not> containthe padding characters typical of Base64 encodings. This omission isdeliberate, and is done to maintain compatibility with the family ofCPAN Digest modules. See L</"PADDING OF BASE64 DIGESTS"> for details.=back=head1 SEE ALSOL<Digest>, L<Digest::SHA::PurePerl>The Secure Hash Standard (FIPS PUB 180-2) can be found at:L<http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf>The Keyed-Hash Message Authentication Code (HMAC):L<http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf>=head1 AUTHOR Mark Shelor <mshelor@cpan.org>=head1 ACKNOWLEDGMENTSThe author is particularly grateful to 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 Woodburyfor their valuable comments and suggestions.=head1 COPYRIGHT AND LICENSECopyright (C) 2003-2007 Mark ShelorThis library is free software; you can redistribute it and/or modifyit under the same terms as Perl itself.L<perlartistic>=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -