-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInMemoryKey.cs
More file actions
48 lines (42 loc) · 946 Bytes
/
InMemoryKey.cs
File metadata and controls
48 lines (42 loc) · 946 Bytes
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
using System;
using System.Security.Cryptography;
namespace Fire.Authentication.Private;
public class InMemoryKey : IKeyProvider
{
private readonly byte[] _keyData;
public InMemoryKey(byte[] key)
{
_keyData = key ?? throw new ArgumentNullException(nameof(key));
if (_keyData.Length <= 0)
{
throw new ArgumentException("The key must not be empty");
}
}
internal byte[] GetCopyOfKey()
{
return (byte[])_keyData.Clone();
}
public byte[] ComputeHmac(OtpHashMode mode, byte[] data)
{
using HMAC hmac = CreateHmacHash(mode);
byte[] key = GetCopyOfKey();
try
{
hmac.Key = key;
return hmac.ComputeHash(data);
}
finally
{
KeyUtilities.Destroy(key);
}
}
private static HMAC CreateHmacHash(OtpHashMode otpHashMode)
{
return otpHashMode switch
{
OtpHashMode.Sha256 => new HMACSHA256(),
OtpHashMode.Sha512 => new HMACSHA512(),
_ => new HMACSHA1() // OtpHashMode.Sha1 or default
};
}
}