-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasherargon2.go
108 lines (92 loc) · 2.6 KB
/
hasherargon2.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
package gocrypto
import (
"crypto/subtle"
"fmt"
"strings"
"golang.org/x/crypto/argon2"
)
type argon2Hasher struct {
memory uint32
iterations uint32
parallelism uint8
saltLength uint32
keyLength uint32
}
func (driver argon2Hasher) Hash(data []byte) ([]byte, error) {
salt, err := NewSimpleKey(SimpleLength(driver.saltLength))
if err != nil {
return nil, err
}
key := argon2.IDKey(
data, salt,
driver.iterations, driver.memory,
driver.parallelism, driver.keyLength,
)
encodedSalt, _ := base64Encode(salt)
encodedKey, _ := base64Encode(key)
return base64Encode(
[]byte(
fmt.Sprintf(
"%s#%d$%d$%d$%d#%s",
string(encodedSalt), argon2.Version, driver.memory,
driver.iterations, driver.parallelism, string(encodedKey),
),
),
)
}
func (driver argon2Hasher) Validate(hash, data []byte) (bool, error) {
salt, key, _, _, _, _, err := driver.decodeHash(hash)
if err != nil {
return false, err
}
otherKey := argon2.IDKey(
data, salt,
driver.iterations, driver.memory,
driver.parallelism, driver.keyLength,
)
// Check that the contents of the hashed passwords are identical. Note
// that we are using the subtle.ConstantTimeCompare() function for this
// to help prevent timing attacks.
if subtle.ConstantTimeCompare(key, otherKey) == 1 {
return true, nil
}
return false, nil
}
// decodeHash Decode hash and extract args
// returns salt, key, version, memory, iterations, parallelism and error
func (driver argon2Hasher) decodeHash(hash []byte) ([]byte, []byte, int, uint32, uint32, uint8, error) {
// Decode
raw, err := base64Decode(hash)
if err != nil {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("invalid password hash")
}
// Extract parts
parts := strings.Split(string(raw), "#")
if len(parts) != 3 {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("invalid password hash")
}
// Parse salt
salt, err := base64Decode([]byte(parts[0]))
if err != nil {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("invalid password hash")
}
// Parse and validate version
var version int
var memory int
var iterations int
var parallelism int
_, err = fmt.Sscanf(parts[1], "%d$%d$%d$%d", &version, &memory, &iterations, ¶llelism)
if err != nil {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("invalid password hash")
}
if version != argon2.Version {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("incompatible version of argon2")
}
// Parse key
key, err := base64Decode([]byte(parts[2]))
if err != nil {
return nil, nil, 0, 0, 0, 0, fmt.Errorf("invalid password hash")
}
// return final result
return salt, key, version, uint32(memory), uint32(iterations), uint8(parallelism), nil
}