-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher.go
74 lines (62 loc) · 1.44 KB
/
hasher.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
package hasher
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"errors"
"golang.org/x/crypto/argon2"
)
const VERSION = 2
func Hash(password string) (string, string, int) {
var salt = newSalt(16)
return toString(hashPasswordV2(password, salt)), toString(salt), VERSION
}
func Verify(password string, hash string, salt string, version int) bool {
var toBytes = toBytesV2
var hashPassword = hashPasswordV2
if version > VERSION || version < 1 {
panic(errors.New("version not supported"))
}
if version == 1 {
toBytes = toBytesV1
hashPassword = hashPasswordV1
}
return subtle.ConstantTimeCompare(
hashPassword(password, toBytes(salt)),
toBytes(hash)) == 1
}
func NeedsRehash(version int) bool {
return version < VERSION
}
func hashPasswordV2(password string, salt []byte) []byte {
return argon2.IDKey([]byte(password), salt, 4, 65536, 2, 32)
}
func hashPasswordV1(password string, salt []byte) []byte {
return argon2.IDKey([]byte(password), salt, 1, 65536, 4, 32)
}
func newSalt(length int) []byte {
salt := make([]byte, length)
_, err := rand.Read(salt)
if err != nil {
panic(err)
}
return salt
}
func toString(b []byte) string {
return hex.EncodeToString(b)
}
func toBytesV2(s string) []byte {
var b, err = hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func toBytesV1(s string) []byte {
var b, err = base64.RawStdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return b
}