encryption

This commit is contained in:
insanity 2017-03-29 12:29:06 +09:00
commit a0383401b1
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package encryption
import (
"crypto/rand"
"golang.org/x/crypto/scrypt"
"io"
)
const (
PW_SALT_BYTES = 32
PW_HASH_BYTES = 64
)
func Encrypt(pw string) ([]byte, []byte, error) {
salt := make([]byte, PW_SALT_BYTES)
_, err := io.ReadFull(rand.Reader, salt)
if err != nil {
return nil, nil, err
}
hash, err := scrypt.Key([]byte(pw), salt, 16384, 8, 1, PW_HASH_BYTES)
if err != nil {
return nil, nil, err
}
return salt, hash, nil
}

View File

@ -0,0 +1,15 @@
package encryption
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)
}