Skip Menu |

This queue is for tickets about the Digest-SHA CPAN distribution.

Report information
The Basics
Id: 42474
Status: rejected
Worked: 5 min
Priority: 0/
Queue: Digest-SHA

People
Owner: Nobody in particular
Requestors: business2008 [...] rodneybeede.com.NOSPAM
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 5.47
Fixed in: (no value)



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";
So the problem with my example Perl was that I ran the command "echo test|sha256sum" which sent the text "test\n". Running "echo -n test|sha256sum" showed the expected results in the Perl module. However the Perl module documentation should stress this point: Calling $digest->addfile("/path/to/some/file"); results in the file being opened by the module in ASCII mode. Thus one MUST not use this method if the content is binary. Instead you should open the file before making the addfile call like: open(FILE_HANDLE, "<", "/path/to/some/file") || die("$!\n"); binmode FILE_HANDLE; $digest->addfile(FILE_HANDLE); close(FILE_HANDLE); It'd be nice if the addfile() call just defaulted to binmode or had a second optional parameter to specify binmode.
I finally found where in the documentation it says addfile() can take an optional mode. I strongly recommend adding an example of the addfile() mode option to the SYNOPSIS. This will save future people like me from making this mistake.
Your test case is incorrect: the "echo" command attaches a newline at the end unless suppressed by the "-n" option. Also, the "addfile" method uses ASCII mode by default so that text files won't trigger digest mismatches between DOS and Unix. You are incorrect in assuming that "addfile" doesn't have an optional parameter to specify binary mode: the documentation clearly spells this out (ref. "b" option). In future, if you have problems getting things to work out, you might first try sending out some emails to make sure you haven't overlooked something simple. This saves everyone time in the long run. Mark
The status of this ticket has been changed to "rejected": the discrepancy cited by the requester reflects a problem with his test case rather than a problem with the Digest::SHA module.