-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrypt.go
55 lines (48 loc) · 1.31 KB
/
scrypt.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
package gopeach
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"strings"
"golang.org/x/crypto/scrypt"
)
const (
// DefaultSaltLength is the default salt length.
DefaultSaltLength = 32
// DefaultHashLength is the default hash length.
DefaultHashLength = 64
)
// Hash hashes a string.
func Hash(s string) (string, error) {
salt := make([]byte, DefaultSaltLength)
_, err := rand.Read(salt)
if err != nil {
return "", err
}
hash, err := scrypt.Key([]byte(s), salt, 1024*DefaultHashLength, 8, 1, DefaultHashLength)
return base64.StdEncoding.EncodeToString(salt) + ":" + base64.StdEncoding.EncodeToString(hash), err // "+"" is 99x faster than "fmt.Sprintf"
}
// Compare compares a string and a hash.
func CompareHash(s, hash string) (bool, error) {
var err error
hparts := strings.Split(hash, ":")
if len(hparts) != 2 {
return false, fmt.Errorf("invalid hash")
}
salt, errs := base64.StdEncoding.DecodeString(hparts[0])
h, errh := base64.StdEncoding.DecodeString(hparts[1])
if errs != nil || errh != nil {
err = errs
if err == nil {
err = errh
}
return false, err
}
hashlen := len(h)
h2, err := scrypt.Key([]byte(s), salt, 1024*hashlen, 8, 1, hashlen)
if err != nil {
return false, err
}
return bytes.Compare(h, h2) == 0, nil
}