-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashingFunctionTests.cs
78 lines (63 loc) · 2.3 KB
/
HashingFunctionTests.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Security.Cryptography;
using System.Text;
namespace App.Tests;
public class HashingFunctionTests
{
private const int OutputLength = 64;
private const int SaltSize = 16;
private readonly HashAlgorithmName HashAlgorithmName = HashAlgorithmName.SHA512;
[Theory]
[InlineData("admin", 100)]
[InlineData("admin", 10000)]
[InlineData("A1b@C#d$e^", 10000)]
[InlineData("A1b@C#d$e^", 100)]
[InlineData("A1b@C#d$e^", 1)]
public void GivenAHashedPasswordGeneratedWithLegacyMethod_MustBeVerifiedWithBCLStaticMethod(string password, int iterations)
{
byte[] saltBytes;
byte[] bytesToVerify;
using (var algorithm = new Rfc2898DeriveBytes(
password,
SaltSize,
iterations,
HashAlgorithmName))
{
bytesToVerify = algorithm.GetBytes(OutputLength);
saltBytes = algorithm.Salt;
}
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
var result = Rfc2898DeriveBytes.Pbkdf2(passwordBytes, saltBytes, iterations, HashAlgorithmName, OutputLength);
Assert.True(result.SequenceEqual(bytesToVerify));
}
[Theory]
[InlineData("admin", 100)]
[InlineData("admin", 10000)]
[InlineData("A1b@C#d$e^", 10000)]
[InlineData("A1b@C#d$e^", 100)]
[InlineData("A1b@C#d$e^", 1)]
public void GivenAHashedPasswordGeneratedWithLegacyMethod_MustBeVerifiedWithNativelyImplemented_CPPHashFunction(string password, int iterations)
{
byte[] saltBytes;
byte[] bytesToVerify;
using (var algorithm = new Rfc2898DeriveBytes(
password,
SaltSize,
iterations,
HashAlgorithmName))
{
bytesToVerify = algorithm.GetBytes(OutputLength);
saltBytes = algorithm.Salt;
}
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
// Allocate memory for output
byte[] outputArray = new byte[OutputLength];
NativeCall.Pbkdf2(passwordBytes,
(IntPtr)passwordBytes.Length,
saltBytes,
(IntPtr)saltBytes.Length,
(uint)iterations,
outputArray,
(IntPtr)OutputLength);
Assert.True(bytesToVerify.SequenceEqual(outputArray));
}
}