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 = 'swaranga@gmail.com';
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.
cbc41284e23c8c7ed98f589b6d6ebfd6
is MD5 for swaranga@gmail.com
, not SHA-256
SHA encryptions for swaranga@gmail.com
>>
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('swaranga@gmail.com')), "\n";
or
#!/usr/bin/perl
use Digest::SHA qw(sha256_hex);
print sha256_hex('swaranga@gmail.com'), "\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('swaranga@gmail.com')), "\n";
or
#!/usr/bin/perl
use Digest::MD5 qw(md5_hex);
print md5_hex('swaranga@gmail.com'), "\n";
Output:
cbc41284e23c8c7ed98f589b6d6ebfd6
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.