Skip to content

Commit 129214b

Browse files
committed
finished basic HMAC class
1 parent cde5555 commit 129214b

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed
Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,64 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Security.Cryptography;
45

56
namespace Encryption_App
67
{
78
class MessageAuthenticator
89
{
9-
public void VerifyHMACFile(byte[] data, byte[] key)
10+
public byte[] CreateHMAC(byte[] data, byte[] key)
1011
{
1112
byte[] hashKey;
1213

1314
using (var hmac = new HMACSHA384(key))
1415
{
15-
if (key.Length > hmac.InputBlockSize)
16-
{
17-
hashKey = hmac.ComputeHash(data);
18-
}
16+
hashKey = hmac.ComputeHash(data);
1917
}
18+
19+
return hashKey;
20+
}
21+
22+
public byte[] CreateHMAC(byte[] data, byte[] key, Type TypeOfHash)
23+
{
24+
HMAC hmac;
25+
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
26+
{
27+
hmac = (HMAC)Activator.CreateInstance(TypeOfHash);
28+
}
29+
else
30+
{
31+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
32+
}
33+
34+
byte[] hashKey;
35+
36+
using (hmac)
37+
{
38+
hashKey = hmac.ComputeHash(data);
39+
}
40+
41+
return hashKey;
42+
}
43+
44+
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash)
45+
{
46+
byte[] hashKey;
47+
48+
using (var hmac = new HMACSHA384(key))
49+
{
50+
hashKey = hmac.ComputeHash(data);
51+
}
52+
53+
if (data.SequenceEqual(hash))
54+
{
55+
return true;
56+
}
57+
58+
return false;
2059
}
2160

22-
public void VerifyHMACFile(byte[] data, byte[] key, Type TypeOfHash)
61+
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type TypeOfHash)
2362
{
2463
HMAC hmac;
2564
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
@@ -30,6 +69,20 @@ public void VerifyHMACFile(byte[] data, byte[] key, Type TypeOfHash)
3069
{
3170
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
3271
}
72+
73+
byte[] hashKey;
74+
75+
using (hmac)
76+
{
77+
hashKey = hmac.ComputeHash(data);
78+
}
79+
80+
if (data.SequenceEqual(hash))
81+
{
82+
return true;
83+
}
84+
85+
return false;
3386
}
3487
}
3588
}

0 commit comments

Comments
 (0)