Skip to content

Commit

Permalink
use pbkdf2 to generate AES CTR Input
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Kaupat committed May 10, 2021
1 parent 6615be4 commit 2a307b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 15 additions & 2 deletions encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ package bip39
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"crypto/sha512"
"golang.org/x/crypto/pbkdf2"
)

func EncryptMnemonic(mnemonic string, password string) (string, error) {
entropy, err := EntropyFromMnemonic(mnemonic)
if err != nil {
return "", err
}
encEntropy, err := EncryptEntropy(entropy, password)
if err != nil {
return "", err
}
return NewMnemonic(encEntropy)
}

func EncryptEntropy(entropy []byte, password string) ([]byte, error) {
pwHash := sha256.Sum256([]byte(password))
pwHash := pbkdf2.Key([]byte(password), []byte("mnemonic-encryption"), 2048, 32, sha512.New)

ci, err := aes.NewCipher(pwHash[:])
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion encyrption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ import (

func TestEcryptMnemonic(t *testing.T) {
mnmonic := "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform"
expectedEncMnemonic := "artist depart host scheme update hen short doctor lemon coffee they walk drill welcome mimic expect renew purse wear slow punch need comic team"
expectedEncMnemonic := "father level place shallow review foil illegal elbow wine warm soft penalty token banner cage century someone warfare horn vote crumble now attack gorilla"
password := "securePassword"

encMnemonic, err := EncryptMnemonic(mnmonic, password)
assert.NoError(t, err)
assert.Equal(t, expectedEncMnemonic, encMnemonic)
}

func TestEcryptEntropy(t *testing.T) {
mnmonic := "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform"
expectedEncMnemonic := "father level place shallow review foil illegal elbow wine warm soft penalty token banner cage century someone warfare horn vote crumble now attack gorilla"
password := "securePassword"

entropy, err := EntropyFromMnemonic(mnmonic)
Expand Down

0 comments on commit 2a307b8

Please sign in to comment.