-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhashutil_test.go
75 lines (60 loc) · 1.39 KB
/
hashutil_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
package hashutil
import (
"crypto"
_ "crypto/sha256"
"testing"
"github.com/stretchr/testify/assert"
)
const (
token = "my_secret_test_token"
)
func TestHashCompare(t *testing.T) {
h := Hasher{
HashFunc: crypto.SHA256,
SaltSize: 128,
}
hash, err := h.Hash(token)
assert.Nil(t, err)
ok, err := Compare(token, hash)
assert.Nil(t, err)
assert.True(t, ok)
// Lets flip some runes in the hash to make it invalid
_hash := []byte(hash)
if _hash[10] == 'a' {
_hash[10] = 'b'
} else {
_hash[10] = 'a'
}
hash = string(_hash)
ok, err = Compare(token, hash)
assert.Nil(t, err)
assert.False(t, ok)
}
func TestHashComparePepper(t *testing.T) {
pepperGetter := func() ([]byte, error) {
return []byte("my_pepper"), nil
}
h := Hasher{
HashFunc: crypto.SHA256,
SaltSize: 128,
PepperGetter: pepperGetter,
}
hash, err := h.Hash(token)
assert.Nil(t, err)
// First try without pepper, which must fail
ok, err := Compare(token, hash)
assert.Nil(t, err)
assert.False(t, ok)
ok, err = Compare(token, hash, pepperGetter)
assert.Nil(t, err)
assert.True(t, ok)
// Lets flip some runes in the hash to make it invalid
_hash := []byte(hash)
_hash[10] = 'a'
_hash[11] = 'b'
_hash[12] = 'c'
hash = string(_hash)
ok, err = Compare(token, hash, pepperGetter)
assert.Nil(t, err)
assert.False(t, ok)
}