diff options
author | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-04-21 23:00:30 +0800 |
---|---|---|
committer | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-05-12 23:22:17 +0800 |
commit | f98e002d9851b8df53a09a0e3189f2e8fdec48df (patch) | |
tree | 611b0f65f80cee91c62716ae18f43aaaed61bc96 /crypto/key_store_passphrase.go | |
parent | 313eec33ad7add82bfdd00e3a076091fa990c799 (diff) | |
download | go-tangerine-f98e002d9851b8df53a09a0e3189f2e8fdec48df.tar.gz go-tangerine-f98e002d9851b8df53a09a0e3189f2e8fdec48df.tar.zst go-tangerine-f98e002d9851b8df53a09a0e3189f2e8fdec48df.zip |
Address pull request comments; key header and hex encoding
* Remove key header from unencrypted key file format and replace
it with a version field
* Change encoding of bytes in key files from base64 to hex
Diffstat (limited to 'crypto/key_store_passphrase.go')
-rw-r--r-- | crypto/key_store_passphrase.go | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index 00717b5d1..2e7929cee 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -68,6 +68,7 @@ import ( "bytes" "crypto/aes" "crypto/cipher" + "encoding/hex" "encoding/json" "errors" "io" @@ -164,15 +165,16 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { mac := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText) cipherStruct := cipherJSON{ - mac, - salt, - iv, + hex.EncodeToString(mac), + hex.EncodeToString(salt), + hex.EncodeToString(iv), keyHeaderJSON, - cipherText, + hex.EncodeToString(cipherText), } keyStruct := encryptedKeyJSON{ - key.Id, - key.Address.Bytes(), + version, + key.Id.String(), + hex.EncodeToString(key.Address[:]), cipherStruct, } keyJSON, err := json.Marshal(keyStruct) @@ -190,7 +192,7 @@ func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err return err } - keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex()) + keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr[:])) return os.RemoveAll(keyDirPath) } @@ -203,12 +205,28 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key keyProtected := new(encryptedKeyJSON) err = json.Unmarshal(fileContent, keyProtected) - keyId = keyProtected.Id - mac := keyProtected.Crypto.MAC - salt := keyProtected.Crypto.Salt - iv := keyProtected.Crypto.IV + keyId = uuid.Parse(keyProtected.Id) + + mac, err := hex.DecodeString(keyProtected.Crypto.MAC) + if err != nil { + return nil, nil, err + } + + salt, err := hex.DecodeString(keyProtected.Crypto.Salt) + if err != nil { + return nil, nil, err + } + + iv, err := hex.DecodeString(keyProtected.Crypto.IV) + if err != nil { + return nil, nil, err + } + keyHeader := keyProtected.Crypto.KeyHeader - cipherText := keyProtected.Crypto.CipherText + cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText) + if err != nil { + return nil, nil, err + } // used in MAC keyHeaderJSONStr, err := json.Marshal(keyHeader) |