-
Notifications
You must be signed in to change notification settings - Fork 1
/
HMAC.cs
52 lines (43 loc) · 1.25 KB
/
HMAC.cs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void Main()
{
string message;
string key;
string pubKey;
pubKey = "bye";
key = "hello";
message = "testing123";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
//HMACMD5 hmacmd5 = new HMACMD5(keyByte);
HMACSHA256 hmacsha1 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
var hash = ByteToString(hashmessage);
hash.Dump();
var test = CheckHMAC("testing123", hash, key);
test.Dump();
}
// Define other methods and classes here
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary);
}
public static string CheckHMAC(string message, string oHash, string key)
{
string pubKey;
pubKey = "bye";
key = "hello";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
//HMACMD5 hmacmd5 = new HMACMD5(keyByte);
HMACSHA256 hmacsha1 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
var hash = ByteToString(hashmessage);
return hash;
}