Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 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.  
  13.  
  14. // Create secureHash on string
  15. string hexHash = "";
  16. using (HMACSHA256 hasher = new HMACSHA256(convertedHash))
  17. {
  18. byte[] hashValue = hasher.ComputeHash(Encoding.UTF8.GetBytes(message));
  19. foreach (byte b in hashValue)
  20. {
  21. hexHash += b.ToString("X2");
  22. }
  23. }
  24. return hexHash;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement