-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasher_test.go
156 lines (132 loc) · 3.48 KB
/
hasher_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package hasher
import "testing"
func TestNew(t *testing.T) {
hasher, err := New(1000, 128, 256, HashSHA256)
if err != nil {
t.Errorf("didn't expect to get an error: %v", err)
return
}
if hasher == nil {
t.Errorf("didn't expect a nin-pointer for hasher")
return
}
t.Run("Invalid Iteration Count", func(t *testing.T) {
_, err := New(0, 128, 256, HashSHA256)
if err != ErrInvalidIterationCount {
t.Errorf("expected '%v' but got '%v'", ErrInvalidIterationCount, err)
}
})
t.Run("Invalid Salt Size", func(t *testing.T) {
// negative salt size
_, err := New(1000, -1, 256, HashSHA256)
if err != ErrInvalidSaltSize {
t.Errorf("expected '%v' bot got '%v'", ErrInvalidSaltSize, err)
}
// not a multiple of 8
_, err = New(1000, 14, 256, HashSHA256)
if err != ErrInvalidSaltSize {
t.Errorf("expected '%v' bot got '%v'", ErrInvalidSaltSize, err)
}
})
t.Run("Invalid Key Size", func(t *testing.T) {
// negative key size
_, err := New(1000, 128, -1, HashSHA256)
if err != ErrInvalidKeySize {
t.Errorf("expected '%v' bot got '%v'", ErrInvalidKeySize, err)
}
// not a multiple of 8
_, err = New(1000, 128, 14, HashSHA256)
if err != ErrInvalidKeySize {
t.Errorf("expected '%v' bot got '%v'", ErrInvalidKeySize, err)
}
})
}
func TestHash(t *testing.T) {
pwd := "MyTestPassword"
hash := Hash([]byte(pwd))
t.Run("Format", func(t *testing.T) {
if hash[0] != formatMarker {
t.Errorf("expected '%v' at the start of the hash but got '%b'", formatMarker, hash[0])
}
})
t.Run("Scan", func(t *testing.T) {
_, iterCnt, saltSize := scanHeader(hash)
if iterCnt != DefaultIterationCount {
t.Errorf("expected an iteration count of %d, but got %d", DefaultIterationCount, iterCnt)
}
if saltSize != DefaultSaltSize/8 {
t.Errorf("expected a salt size of %d, but got %d", DefaultSaltSize/8, saltSize)
}
})
}
func TestAlg(t *testing.T) {
keys := map[string]int{
"SHA256": HashSHA256,
"SHA512": HashSHA512,
}
for name, value := range keys {
t.Run(name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpected panic: %v", r)
}
}()
f := alg(value)
if f == nil {
t.Errorf("expected func() hash.Hash, but got nil")
}
})
}
t.Run("Unsupported", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic")
}
}()
// 237 is not aa recognised key
_ = alg(237)
})
}
func TestVerify(t *testing.T) {
pwd := []byte("MyTestPassword")
hash := Hash(pwd)
ok := Verify(pwd, hash)
if !ok {
t.Errorf("expected hash to be valid")
}
t.Run("Empty Hash", func(t *testing.T) {
ok := Verify(pwd, []byte{})
if ok {
t.Errorf("expected hash to be invalid")
}
})
t.Run("Correct Format", func(t *testing.T) {
hash[0] = 0x23 // invalid format marker
ok := Verify(pwd, hash)
if ok {
t.Errorf("expected hash to be invalid")
}
})
t.Run("Invalid Salt Size", func(t *testing.T) {
hasher, _ := New(DefaultIterationCount, 32, DefaultKeySize, DefaultHashKey)
hash := hasher.Hash(pwd)
ok := Verify(pwd, hash)
if ok {
t.Errorf("expected hash to be invalid")
}
})
t.Run("Invalid Key Size", func(t *testing.T) {
hasher, _ := New(DefaultIterationCount, DefaultSaltSize, 128, DefaultHashKey)
hash := hasher.Hash(pwd)
ok := Verify(pwd, hash)
if ok {
t.Errorf("expected hash to be invalid")
}
})
t.Run("Invalid Hash", func(t *testing.T) {
ok := Verify(pwd, hash[:12])
if ok {
t.Errorf("expected hash to be invalid")
}
})
}