-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
39 lines (33 loc) · 845 Bytes
/
util.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
package salted
import (
"crypto/rand"
"github.com/niktheblak/passwordhash/pkg/hasher"
)
const DefaultSaltLength = 8
func GenerateSalt(n int) ([]byte, error) {
salt := make([]byte, n)
_, err := rand.Read(salt)
return salt, err
}
func EnsureSalt(salt []byte) ([]byte, error) {
if len(salt) == 0 {
return GenerateSalt(DefaultSaltLength)
}
return salt, nil
}
func Split(passwordHash []byte) (password []byte, salt []byte, err error) {
saltLen := int(passwordHash[0])
if saltLen <= 0 || saltLen >= 128 {
// the length byte is likely invalid
err = hasher.ErrInvalidHashPrefix
return
}
if len(passwordHash) <= saltLen+1 {
// the byte slice is too short to contain both the salt and the password hash
err = hasher.ErrInvalidHashPrefix
return
}
salt = passwordHash[:saltLen]
password = passwordHash[saltLen:]
return
}