Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What is the .NET equivalent of the PHP function hash_hmac()
- public function hash($message, $secret)
- {
- return base64_encode(hash_hmac('sha1', $message, $secret));
- }
- byte[] SHA1Hash (byte[] data)
- {
- using (var sha1 = new SHA1CryptoServiceProvider())
- {
- return sha1.ComputeHash(data);
- }
- }
- static string Hash (string message, byte[] secretKey)
- {
- using (HMACSHA1 hmac = new HMACSHA1(secretKey))
- {
- return Convert.ToBase64String(
- hmac.ComputeHash(System.Text.UTF8.GetBytes(message));
- }
- }
- private string Hash(string message, byte[] secretKey)
- {
- byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);
- byte[] hashBytes;
- using (HMACSHA1 hmac = new HMACSHA1(secretKey))
- {
- hashBytes = hmac.ComputeHash(msgBytes);
- }
- var sb = new StringBuilder();
- for (int i = 0; i < hashBytes.Length; i++)
- sb.Append(hashBytes[i].ToString("x2"));
- string hexString = sb.ToString();
- byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(hexString);
- return System.Convert.ToBase64String(toEncodeAsBytes);
- }
Add Comment
Please, Sign In to add comment