-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_argon2_test.go
75 lines (68 loc) · 1.99 KB
/
hash_argon2_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 hasher_test
import (
"testing"
"github.com/lhecker/argon2"
"github.com/matthewhartstonge/hasher"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
)
// TestArgon2Hash ensures that a hash is returned from the Argon2 Hasher
func TestArgon2Hash(t *testing.T) {
h := &hasher.Argon2{
Config: argon2.DefaultConfig(),
}
password := []byte("foo")
hash, err := h.Hash(password)
assert.Nil(t, err)
assert.NotNil(t, hash)
assert.NotEqual(t, hash, password)
}
// TestArgon2HashLibErr purposely causes the underlying argon2 lib to error to ensure it is reported up the stack
func TestArgon2HashLibErr(t *testing.T) {
h := &hasher.Argon2{
Config: argon2.DefaultConfig(),
}
h.Config.MemoryCost = 1
password := []byte("foo")
hash, err := h.Hash(password)
assert.Empty(t, hash)
assert.NotNil(t, err)
assert.NotEqual(t, hash, password)
}
// TestArgon2CompareEquals ensures a password can be verified successfully when decoded
func TestArgon2CompareEquals(t *testing.T) {
h := &hasher.Argon2{
Config: argon2.DefaultConfig(),
}
password := []byte("foo")
hash, err := h.Hash(password)
assert.Nil(t, err)
assert.NotNil(t, hash)
err = h.Compare(hash, password)
assert.Nil(t, err)
}
// TestArgon2CompareEquals ensures a compare errors when a presented clear text password does not match the original
func TestArgon2CompareDifferent(t *testing.T) {
h := &hasher.Argon2{
Config: argon2.DefaultConfig(),
}
password := []byte("foo")
hash, err := h.Hash(password)
assert.Nil(t, err)
assert.NotNil(t, hash)
err = h.Compare(hash, []byte(uuid.NewRandom()))
assert.NotNil(t, err)
}
// TestArgon2HashLibErr purposely causes the underlying argon2 lib to error to ensure it is reported up the stack
func TestArgon2CompareLibErr(t *testing.T) {
h := &hasher.Argon2{
Config: argon2.DefaultConfig(),
}
h.Config.MemoryCost = 1
password := []byte("foo")
hash, err := h.Hash(password)
assert.Empty(t, hash)
assert.NotNil(t, err)
err = h.Compare(hash, []byte(uuid.NewRandom()))
assert.NotNil(t, err)
}