Guest User

Untitled

a guest
Aug 15th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. What is the .NET equivalent of the PHP function hash_hmac()
  2. public function hash($message, $secret)
  3. {
  4. return base64_encode(hash_hmac('sha1', $message, $secret));
  5. }
  6.  
  7. byte[] SHA1Hash (byte[] data)
  8. {
  9. using (var sha1 = new SHA1CryptoServiceProvider())
  10. {
  11. return sha1.ComputeHash(data);
  12. }
  13. }
  14.  
  15. static string Hash (string message, byte[] secretKey)
  16. {
  17. using (HMACSHA1 hmac = new HMACSHA1(secretKey))
  18. {
  19. return Convert.ToBase64String(
  20. hmac.ComputeHash(System.Text.UTF8.GetBytes(message));
  21. }
  22. }
  23.  
  24. private string Hash(string message, byte[] secretKey)
  25. {
  26. byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);
  27. byte[] hashBytes;
  28. using (HMACSHA1 hmac = new HMACSHA1(secretKey))
  29. {
  30. hashBytes = hmac.ComputeHash(msgBytes);
  31. }
  32. var sb = new StringBuilder();
  33. for (int i = 0; i < hashBytes.Length; i++)
  34. sb.Append(hashBytes[i].ToString("x2"));
  35. string hexString = sb.ToString();
  36. byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(hexString);
  37. return System.Convert.ToBase64String(toEncodeAsBytes);
  38. }
Add Comment
Please, Sign In to add comment