Why does Digest :: SHA come up with different hashes than those shown in RFC 4868?

I am trying to write some Perl to interact with hash functions in other languages, namely Java at this point. We found that there appears to be the correct source, RFC 4868 , which includes some test keys and strings along with their hashed values. I am using the following snippet and cannot get Perl to get the same result. I can only assume that I am using it incorrectly - can anyone point me in the right direction?

use Digest::SHA qw(hmac_sha512_hex);
my $key = '0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b';
my $value = '4869205468657265';
print hmac_sha512_hex($value, $key);

      

Output: "4ef7 ... 5d40" although RFC 4868 (and my Java compatriot implementation) returns "87aa ... 6854"

+2


source to share


2 answers


use Digest::SHA qw(hmac_sha512_hex);
my $key = pack('H*','0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b');
my $value = "Hi There";
print hmac_sha512_hex($value, $key);

      

gives

87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854

      



RFC quote:

Key =          0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
               0b0b0b0b                          (20 bytes)

Data =         4869205468657265                  ("Hi There")

PRF-HMAC-SHA-512 = 87aa7cdea5ef619d4ff0b4241a1d6cb0
                   2379f4e2ce4ec2787ad0b30545e17cde
                   daa833b7d6b8a702038b274eaea3f4e4
                   be9d914eeb61f1702e696c203a126854

      

PS Adding '0x'

to a string doesn't make it binary, it makes it start with '0'

and 'x'

; -)

+16


source


The validation key must be 20 bytes, where each byte has a hexadecimal value 0x0B

, not a 40 character string. The test value is a string "Hi There"

, not a string "4869205468657625"

. Try the following:



use Digest::SHA qw(hmac_sha512_hex);
my $key = "\x0b" x 20;
my $value = 'Hi There';
print hmac_sha512_hex($value, $key) . "\n";

      

+12


source







All Articles