Skip to content

Commit 7b650f5

Browse files
authored
Merge pull request #25 from johnkellyoxford/dev
Fixed and added overloads to HMAC
2 parents e3ad967 + e6ac79a commit 7b650f5

File tree

1 file changed

+106
-4
lines changed

1 file changed

+106
-4
lines changed

CryptoTools/MessageAuthenticator.cs

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Security.Cryptography;
45

@@ -7,7 +8,7 @@ namespace CryptoTools
78
/// <summary>
89
/// Used for signing and verifying HMACs
910
/// </summary>
10-
internal class MessageAuthenticator
11+
public class MessageAuthenticator
1112
{
1213
/// <summary>
1314
/// Creates a byte[] hashcode that represents the file and key hashed with SHA384. Do not try and verify this yourself, use the VerifyHMAC() func
@@ -39,7 +40,7 @@ public byte[] CreateHMAC(byte[] data, byte[] key, Type typeOfHash)
3940
HMAC hmac;
4041
if (typeOfHash.IsSubclassOf(typeof(HMAC)))
4142
{
42-
hmac = (HMAC)Activator.CreateInstance(typeOfHash);
43+
hmac = (HMAC)Activator.CreateInstance(typeOfHash, key);
4344
}
4445
else
4546
{
@@ -56,6 +57,55 @@ public byte[] CreateHMAC(byte[] data, byte[] key, Type typeOfHash)
5657
return hashKey;
5758
}
5859

60+
/// <summary>
61+
/// Creates a byte[] hashcode that represents the file and key hashed with SHA384. Do not try and verify this yourself, use the VerifyHMAC() func
62+
/// </summary>
63+
/// <param name="path">A path to the file with the encrypted data</param>
64+
/// <param name="key">A byte[] of the key</param>
65+
/// <returns>A byte[] hash that is the file and key hashed</returns>
66+
public byte[] CreateHMAC(string path, byte[] key)
67+
{
68+
byte[] hashKey;
69+
70+
using (var fHandle = new FileStream(path, FileMode.Open))
71+
using (var hmac = new HMACSHA384(key))
72+
{
73+
hashKey = hmac.ComputeHash(fHandle);
74+
}
75+
76+
return hashKey;
77+
}
78+
79+
/// <summary>
80+
/// Signs a encrypted file and key with a hash algorithm of your choosing. Do not try and verify this yourself, use the VerifyHMAC() func
81+
/// </summary>
82+
/// <param name="path">A path to the file with the encrypted data</param>
83+
/// <param name="key">A byte[] of the key</param>
84+
/// <param name="typeOfHash">typeof() any derivative of the System.Security.Cryptography.HMAC class</param>
85+
/// <returns>A byte[] hash that is the file and key hashed</returns>
86+
public byte[] CreateHMAC(string path, byte[] key, Type typeOfHash)
87+
{
88+
HMAC hmac;
89+
if (typeOfHash.IsSubclassOf(typeof(HMAC)))
90+
{
91+
hmac = (HMAC)Activator.CreateInstance(typeOfHash, key);
92+
}
93+
else
94+
{
95+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptography.HMAC\"");
96+
}
97+
98+
byte[] hashKey;
99+
100+
using (var fHandle = new FileStream(path, FileMode.Open))
101+
using (hmac)
102+
{
103+
hashKey = hmac.ComputeHash(fHandle);
104+
}
105+
106+
return hashKey;
107+
}
108+
59109
/// <summary>
60110
/// A function that verifies a HMAC file with SHA384
61111
/// </summary>
@@ -72,7 +122,7 @@ public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash)
72122
hashKey = hmac.ComputeHash(data);
73123
}
74124

75-
return hashKey.SequenceEqual(hash);
125+
return hash.SequenceEqual(hashKey);
76126
}
77127

78128

@@ -103,7 +153,59 @@ public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type typeOfHash)
103153
hashKey = hmac.ComputeHash(data);
104154
}
105155

106-
return data.SequenceEqual(hashKey); // returns true if they match
156+
return hash.SequenceEqual(hashKey); // returns true if they match
157+
}
158+
159+
/// <summary>
160+
/// A function that verifies a HMAC file with SHA384
161+
/// </summary>
162+
/// <param name="path">A path to the file with the encrypted data</param>
163+
/// <param name="key">A byte[] of the key</param>
164+
/// <param name="hash">The hash in the header file/the hash provided, that's been hashed with SHA384</param>
165+
/// <returns>True if they match, otherwise false</returns>
166+
public bool VerifyHMAC(string path, byte[] key, byte[] hash)
167+
{
168+
byte[] hashKey;
169+
170+
using (var fHandle = new FileStream(path, FileMode.Open))
171+
using (var hmac = new HMACSHA384(key))
172+
{
173+
hashKey = hmac.ComputeHash(fHandle);
174+
}
175+
176+
return hash.SequenceEqual(hashKey);
177+
}
178+
179+
180+
/// <summary>
181+
/// A function that verifies a HMAC file with a hash algorithm of your choice
182+
/// </summary>
183+
/// <param name="path">A path to the file with the encrypted data</param>
184+
/// <param name="key">A byte[] of the key</param>
185+
/// <param name="hash">The hash in the header file/the hash provided, that's been hashed with typeOfHash</param>
186+
/// <param name="typeOfHash">typeof() the hash algorithm you used to create this, derived from System.Security.Cryptography.HMAC</param>
187+
/// <returns>True if they match, otherwise false</returns>
188+
public bool VerifyHMAC(string path, byte[] key, byte[] hash, Type typeOfHash)
189+
{
190+
HMAC hmac;
191+
if (typeOfHash.IsSubclassOf(typeof(HMAC)))
192+
{
193+
hmac = (HMAC)Activator.CreateInstance(typeOfHash, key);
194+
}
195+
else
196+
{
197+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptography.HMAC\"");
198+
}
199+
200+
byte[] hashKey;
201+
202+
using (var fHandle = new FileStream(path, FileMode.Open))
203+
using (hmac)
204+
{
205+
hashKey = hmac.ComputeHash(fHandle);
206+
}
207+
208+
return hash.SequenceEqual(hashKey); // returns true if they match
107209
}
108210
}
109211
}

0 commit comments

Comments
 (0)