-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathTest.cs
95 lines (84 loc) · 2.87 KB
/
Test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Text;
using PasswordSecurity;
class Test
{
public static void Main()
{
truncatedHashTest();
basicTests();
testHashFunctionChecking();
}
// Make sure truncated hashes don't validate.
private static void truncatedHashTest()
{
string userString = "test_password";
string goodHash = PasswordStorage.CreateHash(userString);
string badHash = "";
int badHashLength = goodHash.Length;
do {
badHashLength -= 1;
badHash = goodHash.Substring(0, badHashLength);
bool raised = false;
try {
PasswordStorage.VerifyPassword(userString, badHash);
} catch (InvalidHashException) {
raised = true;
}
if (!raised) {
Console.WriteLine("Truncated hash test: FAIL " +
"(At hash length of " + badHashLength + ")");
System.Environment.Exit(1);
}
// The loop goes on until it is two characters away from the last : it
// finds. This is because the PBKDF2 function requires a hash that's at
// least 2 characters long. This will be changed once exceptions are
// implemented.
} while (badHash[badHashLength - 3] != ':');
Console.WriteLine("Truncated hash test: pass");
}
private static void basicTests()
{
// Test password validation
bool failure = false;
for(int i = 0; i < 10; i++)
{
string password = "" + i;
string hash = PasswordStorage.CreateHash(password);
string secondHash = PasswordStorage.CreateHash(password);
if(hash == secondHash) {
Console.WriteLine("Hashes of same password differ: FAIL");
failure = true;
}
String wrongPassword = ""+(i+1);
if(PasswordStorage.VerifyPassword(wrongPassword, hash)) {
Console.WriteLine("Validate wrong password: FAIL");
failure = true;
}
if(!PasswordStorage.VerifyPassword(password, hash)) {
Console.WriteLine("Correct password: FAIL");
failure = true;
}
}
if(failure) {
System.Environment.Exit(1);
}
}
private static void testHashFunctionChecking()
{
string hash = PasswordStorage.CreateHash("foobar");
hash = hash.Replace("sha1:", "sha256:");
bool raised = false;
try {
PasswordStorage.VerifyPassword("foobar", hash);
} catch (CannotPerformOperationException) {
raised = true;
}
if (raised) {
Console.WriteLine("Algorithm swap: pass");
} else {
Console.WriteLine("Algorithm swap: FAIL");
System.Environment.Exit(1);
}
}
}