Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA256 digest in perl

I need to do SHA256 hashing of email addresses and I need the result as a String.

I tried the following:

  use Digest::SHA qw(sha256);
  my $data = '[email protected]';
  my $digest = sha256($data);

  print $digest;

But it prints something like:

B/D6i1μû^Þ©Q;¢Þ

I need the output as follows:

cbc41284e23c8c7ed98f589b6d6ebfd6

The above hash is generated using SHA256 generator of Apache DigestUtils.

What am I doing wrong? I am a newbie in perl, so excuse if it is something silly.

Thanks.

like image 418
Swaranga Sarma Avatar asked Apr 03 '12 10:04

Swaranga Sarma


People also ask

What is the use of Sha in Perl?

Digest::SHA - Perl extension for SHA-1/224/256/384/512 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, SHA-512, SHA-512/224, and SHA-512/256 message digests.

How do I display the SHA-1 digest of a string?

Here's how to display its SHA-1 digest: Note that for larger bit-strings, it's more efficient to use the two-argument version add_bits ($data, $nbits), where $data is in the customary packed binary format used for Perl strings. The module also lets you save intermediate SHA states to a string.

What is the use of Digest method in Sha?

Note that the digest 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 $sha->clone->digest if it's necessary to preserve the original digest state. Returns the digest encoded as a hexadecimal string.

How does Digest::Sha handle Unicode strings?

The rule by which Digest::SHA handles a Unicode string is easy to state, but potentially confusing to grasp: the string is interpreted as a sequence of byte values, where each byte value is equal to the ordinal value (viz. code point) of its corresponding Unicode character.


2 Answers

cbc41284e23c8c7ed98f589b6d6ebfd6 is MD5 for [email protected], not SHA-256


SHA encryptions for [email protected] >>

 SHA-1:            3a3be7013e297e28d24979aadc4ae75d84ce0844
 SHA-256:          0947300f280d422f4418366931cebcfbd17f5ede1507a951153b0f15a21c10de
 SHA-384:          34c01f3956aac32aacae1a6cf67f8a66d441af06c9d36f580ce4be5b234b5399cd879231c49db5bec269309582c19432
 SHA-512:          db1aa053dd9ee191b091abbcb8bead2ec69a1ab2664bb1deeeedbdb49b25e7bc7680a7659ae88c046afdabf1e35ed0e068763f8754b369bfade69cf21f65d166
 SHA-1   (Base64): OjvnAT4pfijSSXmq3ErnXYTOCEQ=
 SHA-256 (Base64): CUcwDygNQi9EGDZpMc68+9F/Xt4VB6lRFTsPFaIcEN4=
 SHA-384 (Base64): NMAfOVaqwyqsrhps9n+KZtRBrwbJ029YDOS+WyNLU5nNh5IxxJ21vsJpMJWCwZQy
 SHA-512 (Base64): 2xqgU92e4ZGwkau8uL6tLsaaGrJmS7He7u29tJsl57x2gKdlmuiMBGr9q/HjXtDgaHY/h1Szab+t5pzyH2XRZg==

If you sure you want to use SHA-256 and you are looking for HEX output, then try this one:

Script:

#!/usr/bin/perl
use Digest::SHA qw(sha256);
print unpack("H*", sha256('[email protected]')), "\n";

or

#!/usr/bin/perl
use Digest::SHA qw(sha256_hex);
print sha256_hex('[email protected]'), "\n";

Output:

0947300f280d422f4418366931cebcfbd17f5ede1507a951153b0f15a21c10de

And if you want MD5 with HEX output, then try this one:

Script:

#!/usr/bin/perl
use Digest::MD5 qw(md5);
print unpack("H*", md5('[email protected]')), "\n";

or

#!/usr/bin/perl
use Digest::MD5 qw(md5_hex);
print md5_hex('[email protected]'), "\n";

Output:

cbc41284e23c8c7ed98f589b6d6ebfd6
like image 177
Ωmega Avatar answered Sep 21 '22 23:09

Ωmega


You probably want Digest::SHA qw(sha256_hex) From CPAN's Digest::SHA page

Logically joins the arguments into a single string, and returns its SHA-1/224/256/384/512 digest encoded as a hexadecimal string.

like image 40
charlesbridge Avatar answered Sep 21 '22 23:09

charlesbridge