-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrypt_test.go
76 lines (70 loc) · 1.79 KB
/
scrypt_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
package kdfcrypt
import (
"encoding/hex"
"testing"
)
var scryptEgs = []struct {
cost int
blockSize int
parallelization int
salt string
hashLength uint32
password string
scryptResult string
}{
{
cost: 16384,
blockSize: 8,
parallelization: 1,
salt: "abcdefgh",
hashLength: 32,
password: "This_is_1_Password_Example!",
scryptResult: "9638fb20584c46380657b4d305f73e2fbc21217da23f8394ff93a3fa290a0191",
},
{
cost: 32768,
blockSize: 16,
parallelization: 3,
salt: "pIBFX2B05zuR7DSl",
hashLength: 32,
password: "sdOrYO5wvpQXF8tAxNC7Jt",
scryptResult: "18f5d6505a6e96a3be3bc593f1b4665b48203732652c68d99a656fb336866d78",
},
}
func TestScrypt(t *testing.T) {
for _, scryptEg := range scryptEgs {
kdf := &Scrypt{
Cost: scryptEg.cost,
BlockSize: scryptEg.blockSize,
Parallelization: scryptEg.parallelization,
}
kdf.SetDefaultParam()
hashed, err := kdf.Derive([]byte(scryptEg.password), []byte(scryptEg.salt), scryptEg.hashLength)
if err != nil {
t.Errorf("Scrypt generate error: %s", err)
}
hashedHex := hex.EncodeToString(hashed)
if hashedHex != scryptEg.scryptResult {
t.Errorf(`Scrypt get wrong result for "%s": %s | %s`, scryptEg.password, hashedHex, scryptEg.scryptResult)
}
}
}
func TestEncodeFromScrypt(t *testing.T) {
kdf := &Scrypt{
Cost: 32768,
BlockSize: 8,
Parallelization: 1,
}
kdf.SetDefaultParam()
encoded, err := EncodeFromKDF(pwEg, kdf, "A_fixed-salt+123", 32)
if err != nil {
t.Fatalf("Encode from KDF error: %s", err)
}
match, err := Verify(pwEg, encoded)
if err != nil {
t.Fatalf("Verify error: %s", err)
}
if !match {
t.Error("Verify does not match")
}
}