Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Digest sha256 returns gibberish

by invalid_logic (Initiate)
on Aug 08, 2012 at 02:41 UTC ( #986114=perlquestion: print w/replies, xml ) Need Help??
invalid_logic has asked for the wisdom of the Perl Monks concerning the following question:

I was able to get Digest::SHA working but it's returning gibberish. I'm not sure why. $data1 and $data2 are byte strings in hex. There is no '0x' preceeding the byte strings.

$digest = sha256_hex($data1 . $data2); $digest2 = sha256($data1 . $data2);

With '$digest' I get a valid result, but with 'digest2' I get gibberish.

print "This is a hexdigest->" . $digest, "\n"; print "This is a digest->" . $digest2, "\n"; This is a digest->��j6��Uϕ�&# +65533;Y�b�S�A��i�6�& +#1142;�,

What did I do wrong?

Replies are listed 'Best First'.
Re: Digest sha256 returns gibberish
by Athanasius (Chancellor) on Aug 08, 2012 at 03:16 UTC

    From Digest1:

    • binary
    This is the most compact form, but it is not well suited for printing or embedding in places that can't handle arbitrary data.

    Consider using sha256_base64, or stick with sha256_hex:

    #! perl use strict; use warnings; use Digest::SHA qw(sha256_hex sha256_base64); my $data1 = '0123456789ABCDEF'; my $data2 = 'FEDCBA9876543210'; my $digest1 = sha256_hex ($data1 . $data2); my $digest3 = sha256_base64($data1 . $data2); print "This is a hexdigest->" . $digest1, "\n"; print "This is a base64digest->" . $digest3, "\n";


    This is a hexdigest->b4a5e0ad9fce13bf90bcb09147ba9200431389e28fa40eaa2 +a0dcd41d6544470 This is a base64digest->tKXgrZ/OE7+QvLCRR7qSAEMTieKPpA6qKg3NQdZURHA

    What output format do you need?

    1 Referencing Digest, because SHA says: “The programming interface is easy to use: it’s the same one found in CPAN’s Digest module.”

    Athanasius <°(((><contra mundum

      I'm hashing in order to verify pre-existing hash values that were generated with C++. The process and each steps results are documented but I'm following along in perl. So whatever the result I get, should match the authors examples.

      The following is the concatenated result:


      Which needs to match the following string after hashing with sha256*:


      I've tried with sha256_base64 and it doesn't match.

        You need to calculate sha256 on the binary form of this data, not on the hexademical string:
        $ perl '-MDigest::SHA "sha256_hex"' -E'say sha256_hex(pack("H*","800C2 +8FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"))' 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592
        Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986114]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-20 04:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.