-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
password.go
97 lines (84 loc) 路 2.46 KB
/
password.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
package utils
import (
"crypto/rand"
"crypto/subtle"
"github.com/Mangatsu/server/pkg/log"
"go.uber.org/zap"
"golang.org/x/crypto/argon2"
)
// Argon2idHash is a wrapper for the argon2id hashing algorithm.
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#password-hashing-algorithms
type Argon2idHash struct {
// time represents the number of passed over the specified memory
time uint32
// CPU memory cost
memory uint32
// threads for parallelism
threads uint8
// keyLen of the generate hash key
keyLen uint32
// saltLen the length of the salt used
saltLen uint32
}
// HashSalt is a wrapper for the hash and salt values.
type HashSalt struct {
Hash []byte
Salt []byte
}
// NewArgon2idHash constructor function for Argon2idHash.
func NewArgon2idHash(time, saltLen, memory uint32, threads uint8, keyLen uint32) *Argon2idHash {
return &Argon2idHash{
// time represents the number of passed over the specified memory
time: time,
// salt length
saltLen: saltLen,
// CPU memory cost (in KiB)
memory: memory,
// threads for parallelism
threads: threads,
// hash key length
keyLen: keyLen,
}
}
// DefaultArgon2idHash constructor function for Argon2idHash.
// https://tobtu.com/minimum-password-settings/
func DefaultArgon2idHash() *Argon2idHash {
return &Argon2idHash{
time: 2,
saltLen: 16,
memory: 19456,
threads: 2,
keyLen: 32,
}
}
// randomSecret generates a random secret of a given length. Used for salt generation.
func randomSecret(length uint32) ([]byte, error) {
secret := make([]byte, length)
_, err := rand.Read(secret)
if err != nil {
return nil, err
}
return secret, nil
}
// GenerateHash using the password and provided salt.
// If no salt value was provided, fallbacks to a random value of a given length.
func (a *Argon2idHash) GenerateHash(password, salt []byte) (*HashSalt, error) {
if len(salt) == 0 {
var err error
if salt, err = randomSecret(a.saltLen); err != nil {
return nil, err
}
}
hash := argon2.IDKey(password, salt, a.time, a.memory, a.threads, a.keyLen)
return &HashSalt{Hash: hash, Salt: salt}, nil
}
// Compare generated hash with stored hash.
func (a *Argon2idHash) Compare(hash, salt, password []byte) bool {
hashSalt, err := a.GenerateHash(password, salt)
if err != nil {
log.Z.Debug("failed to generate hash", zap.Error(err))
return false
}
// ConstantTimeCompare used to prevent timing attacks.
return subtle.ConstantTimeCompare(hash, hashSalt.Hash) == 1
}