1

I am trying to create a base64-encoded HMAC SHA512 hash using a secret of secret and a payload of foo. I am unable to make my .NET code produce the correct value. I am wondering if the encoding is the underlying issue.

Code:

UTF8Encoding encoding = new UTF8Encoding();
HMACSHA512 hmac = new HMACSHA512(encoding.GetBytes("secret")); // init the HMAC hash with "secret" as a byte array
byte[] hash = hmac.ComputeHash(encoding.GetBytes("foo")); // hash the payload
String result = Convert.ToBase64String(hash); // Base64 encode the payload

The incorrect hash & base64 result:

gt9xA96Ngt5F4BxF/mQrXRPGwrR97K/rwAlDHGZcb6Xz0a9Ol46hvekUJmIgc+vqxho0Ye/UZ+CXHHiLyOvbvg==

The expected hash & base64 result:

ODJkZjcxMDNkZThkODJkZTQ1ZTAxYzQ1ZmU2NDJiNWQxM2M2YzJiNDdkZWNhZmViYzAwOTQzMWM2NjVjNmZhNWYzZDFhZjRlOTc4ZWExYmRlOTE0MjY2MjIwNzNlYmVhYzYxYTM0NjFlZmQ0NjdlMDk3MWM3ODhiYzhlYmRiYmU=

mark
  • 1,953
  • 1
  • 24
  • 47
  • 1
    If you are not generating the "expected" result, you must be getting it from somewhere else. Where are seeing this? – dana Mar 25 '16 at 15:45
  • @dana I've got a node.js program and a php program producing the correct hash. – mark Mar 25 '16 at 15:47

1 Answers1

2

Since second version is just Base64 of Hex representation you need to convert byte array to hex first and than Base64 ASCII version of the string.

Steps:

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • This was it. I had seen the questions you referenced, but it didn't make sense that it was relevant until you said that I needed to convert the byte[] to hex first. Thank you. – mark Mar 25 '16 at 15:57