.
This commit is contained in:
parent
0f658e598c
commit
ec7379a2ec
|
@ -8,30 +8,31 @@ import (
|
|||
|
||||
const (
|
||||
PW_SALT_BYTES = 32
|
||||
PW_HASH_BYTES = 64
|
||||
PW_HASH_BYTES = 32
|
||||
)
|
||||
|
||||
func Encrypt(pw string) ([]byte, []byte, error) {
|
||||
func Encrypt(pw string) (string, string, error) {
|
||||
|
||||
salt := make([]byte, PW_SALT_BYTES)
|
||||
_, err := io.ReadFull(rand.Reader, salt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
hash, err := scrypt.Key([]byte(pw), salt, 16384, 8, 1, PW_HASH_BYTES)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return salt, hash, nil
|
||||
return string(salt), string(hash), nil
|
||||
}
|
||||
|
||||
func Check(pw, savedSalt, savedDigest string) bool {
|
||||
|
||||
hash, err := scrypt.Key([]byte(pw), []byte(savedSalt), 16384, 8, 1, PW_HASH_BYTES)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if string(hash) == savedDigest {
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -4,12 +4,16 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
salt, hash, err := Encrypt("MyPassword")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("salt : %x\n", salt)
|
||||
t.Logf("hash : %x\n", hash)
|
||||
|
||||
b:= Check("MyPassword", salt, hash)
|
||||
t.Log(b)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user