From a3059cfa3812d2f41528a45b063990a235f421a4 Mon Sep 17 00:00:00 2001 From: "insanity@loafle.com" Date: Fri, 23 Jun 2017 17:48:56 +0900 Subject: [PATCH] return readable string --- encryption.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/encryption.go b/encryption.go index 13df5e9..2791120 100644 --- a/encryption.go +++ b/encryption.go @@ -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 -} \ No newline at end of file +}