code_junkie

Why would HMAC SHA-1 return a different digest with the same input?

Nov 12th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use MIME::Base64 qw(encode_base64);
  4. use Digest::HMAC_SHA1;
  5. use String::Escape qw( printable unprintable );
  6.  
  7. sub _ascii_to_hex {
  8. (my $str = shift) =~ s/(.|n)/sprintf("%02lx", ord $1)/eg;
  9. return $str;
  10. }
  11.  
  12. sub _encode {
  13. my ( $aws_secret_access_key, $str ) = @_;
  14. print "secret key hex: "._ascii_to_hex($aws_secret_access_key)."n";
  15. my $hmac = Digest::HMAC_SHA1->new($aws_secret_access_key);
  16. $hmac->add($str);
  17. my $digest = $hmac->digest;
  18. print "cleartext hex: "._ascii_to_hex($str)."n";
  19. print "digest hex: "._ascii_to_hex($digest)."n";
  20. my $b64 = encode_base64( $digest, '' );
  21. print "encoded: ".$b64."n";
  22. }
  23.  
  24. my $secret = "abcd1234";
  25. my $cleartext = "GETnnnFri, 12 Dec 2008 10:08:51 GMT+00:00n/";
  26. _encode($secret, $cleartext);
  27.  
  28. $ ./testhmac.pl
  29. secret key hex: 6162636431323334
  30. cleartext hex: 4745540a0a0a4672692c2031322044656320323030382031303a30383a353120474d542b30303a30300a2f
  31. digest hex: 63308f9b8a198440d6d8685a3f3f70d0aab02f68
  32. encoded: YzCPm4oZhEDW2GhaPz9w0KqwL2g=
  33.  
  34. $ s3ls --access-key=foobar --secret-key=abcd1234
  35. ...
  36. secret key hex: 6162636431323334
  37. cleartext hex: 4745540a0a0a4672692c2031322044656320323030382031303a30383a353120474d542b30303a30300a2f
  38. digest hex: c0da50050c451847de7ed055c5286de584527a22
  39. encoded: wNpQBQxFGEfeftBVxSht5YRSeiI=
  40.  
  41. use Digest::SHA qw(hmac_sha1_hex);
  42. my $hash = hmac_sha1_hex($data, $key);
  43.  
  44. secret key hex: abcd...1234
  45.  
  46. _ascii_to_hex("blahblahblah")
Add Comment
Please, Sign In to add comment