Guest User

Untitled

a guest
Jul 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public static string CreateSHA256Signature(string key, string message)
  2. {
  3. // Hex Decode the Secure Secret for use in using the HMACSHA256 hasher
  4. // hex decoding eliminates this source of error as it is independent of the character encoding
  5. // hex decoding is precise in converting to a byte array and is the preferred form for representing binary values as hex strings.
  6. var convertedHash = new byte[key.Length / 2];
  7. for (var i = 0; i < key.Length / 2; i++)
  8. {
  9. convertedHash[i] = (byte)int.Parse(key.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
  10. }
  11.  
  12. // Create secureHash on string
  13. var hexHash = "";
  14. using (var hasher = new HMACSHA256(convertedHash))
  15. {
  16. var hashValue = hasher.ComputeHash(Encoding.UTF8.GetBytes(message));
  17. hexHash = hashValue.Aggregate(hexHash, (current, b) => current + b.ToString("X2"));
  18. }
  19. return hexHash;
  20. }
Add Comment
Please, Sign In to add comment