-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHmacsha256.cs
More file actions
38 lines (33 loc) · 1.15 KB
/
Copy pathHmacsha256.cs
File metadata and controls
38 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private static string HMACSHA256(string message, string key)
{
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacSHA256 = new HMACSHA256(keyByte))
{
byte[] hashMessage = hmacSHA256.ComputeHash(messageBytes);
return BitConverter.ToString(hashMessage).Replace("-", "").ToLower();
}
}
HMACSHA256("TEST", "KEY");
// resulg : 615dac1c53c9396d8f69a419a0b2d9393a0461d7ad5f7f3d9beb57264129ef12
//ExtensionMethods
public static class ExtensionMethods
{
public static string HMACSHA256(this string message, string key)
{
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacSHA256 = new HMACSHA256(keyByte))
{
byte[] hashMessage = hmacSHA256.ComputeHash(messageBytes);
return BitConverter.ToString(hashMessage).Replace("-", "").ToLower();
}
}
}
static void Main(string[] args)
{
var result = "TEST".HMACSHA256("KEY");
Console.WriteLine(result);
}