Subject: | Encoding different from shaXXXsum and other sha tools |
When I encode any data (binary or text) with the Perl Digest::SHA library the
result is different than any results I get when using Linux's shaXXXsum
command or the Java MessageDigest library.
Windows XP 64-bit w/SP2
ActiveState Perl v5.10.0 built for MSWin32-x86-multi-thread
Binary build 1004 [287188] provided by ActiveState http://www.ActiveState.com
Built Sep 3 2008 13:16:37
Digest::SHA 5.47
Example sha256sum command
echo test|sha256sum
f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
Example Perl script that does not result in the expected above hash:
use strict;
use Digest::SHA qw(sha256 sha256_hex sha256_base64 hmac_sha256
hmac_sha256_hex hmac_sha256_base64
sha512 sha512_hex sha512_base64 hmac_sha512 hmac_sha512_hex
hmac_sha512_base64
);
use constant SAMPLE => "test";
# from Linux shaXXXsum command
use constant EXPECTED_sha256sum =>
"f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2";
use constant EXPECTED_sha512sum =>
"0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea1
25dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123";
# Prepare the output file
print "\n";
print "sha256(\$data, ...) \t" . sha256(SAMPLE) . "\n";
print "sha256_hex(\$data, ...) \t" . sha256_hex(SAMPLE) . "\n";
print "sha256_base64(\$data, ...) \t" . sha256_base64(SAMPLE) . "\n";
print "hmac_sha256(\$data, ...) \t" . hmac_sha256(SAMPLE) . "\n";
print "hmac_sha256_hex(\$data, ...) \t" . hmac_sha256_hex(SAMPLE) .
"\n";
print "hmac_sha256_base64(\$data, ...) \t" . hmac_sha256_base64(SAMPLE) .
"\n";
print "\n";
print "EXPECTED: \t" . EXPECTED_sha256sum . "\n";
print "\n";
print "\n";
print "sha512(\$data, ...) \t" . sha512(SAMPLE) . "\n";
print "sha512_hex(\$data, ...) \t" . sha512_hex(SAMPLE) . "\n";
print "sha512_base64(\$data, ...) \t" . sha512_base64(SAMPLE) . "\n";
print "hmac_sha512(\$data, ...) \t" . hmac_sha512(SAMPLE) . "\n";
print "hmac_sha512_hex(\$data, ...) \t" . hmac_sha512_hex(SAMPLE) .
"\n";
print "hmac_sha512_base64(\$data, ...) \t" . hmac_sha512_base64(SAMPLE) .
"\n";
print "\n";
print "EXPECTED: \t" . EXPECTED_sha512sum . "\n";
print "\n";