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 ( import (
"crypto/rand" "crypto/rand"
"encoding/hex"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
"io" "io"
) )
const ( const (
PW_SALT_BYTES = 32 PW_SALT_BYTES = 64
PW_HASH_BYTES = 32 PW_HASH_BYTES = 64
) )
func Encrypt(pw string) (string, string, error) { func Encrypt(pw string) (string, string, error) {
@ -23,17 +24,22 @@ func Encrypt(pw string) (string, string, error) {
if err != nil { if err != nil {
return "", "", err 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 { 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 { if err != nil {
return false return false
} }
new := hex.EncodeToString(hash)
if string(hash) == savedDigest { if new == savedDigest {
return true return true
} }
return false return false