return readable string

This commit is contained in:
insanity@loafle.com 2017-06-23 17:48:56 +09:00
parent ec7379a2ec
commit a3059cfa38

View File

@ -2,13 +2,14 @@ package encryption
import (
"crypto/rand"
"encoding/hex"
"golang.org/x/crypto/scrypt"
"io"
)
const (
PW_SALT_BYTES = 32
PW_HASH_BYTES = 32
PW_SALT_BYTES = 64
PW_HASH_BYTES = 64
)
func Encrypt(pw string) (string, string, error) {
@ -23,18 +24,23 @@ func Encrypt(pw string) (string, string, error) {
if err != nil {
return "", "", err
}
return string(salt), string(hash), nil
saltStr := hex.EncodeToString(salt)
hashStr := hex.EncodeToString(hash)
return saltStr, hashStr, nil
}
func Check(pw, savedSalt, savedDigest string) bool {
salt, err := hex.DecodeString(savedSalt)
hash, err := scrypt.Key([]byte(pw), []byte(savedSalt), 16384, 8, 1, PW_HASH_BYTES)
hash, err := scrypt.Key([]byte(pw), salt, 16384, 8, 1, PW_HASH_BYTES)
if err != nil {
return false
}
new := hex.EncodeToString(hash)
if string(hash) == savedDigest {
if new == savedDigest {
return true
}
return false
}
}