mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
accounts/keystore; unmarshalling chore
This commit is contained in:
parent
509a64ffb9
commit
cd665007a7
2 changed files with 33 additions and 31 deletions
|
|
@ -78,12 +78,22 @@ type encryptedKeyJSONV1 struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CryptoJSON struct {
|
type CryptoJSON struct {
|
||||||
Cipher string `json:"cipher"`
|
Cipher string `json:"cipher"`
|
||||||
CipherText string `json:"ciphertext"`
|
CipherText string `json:"ciphertext"`
|
||||||
CipherParams cipherparamsJSON `json:"cipherparams"`
|
CipherParams cipherparamsJSON `json:"cipherparams"`
|
||||||
KDF string `json:"kdf"`
|
KDF string `json:"kdf"`
|
||||||
KDFParams map[string]interface{} `json:"kdfparams"`
|
KDFParams kdfParamsJSON `json:"kdfparams"`
|
||||||
MAC string `json:"mac"`
|
MAC string `json:"mac"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type kdfParamsJSON struct {
|
||||||
|
Salt string `json:"salt"`
|
||||||
|
DKlen int `json:"dklen"`
|
||||||
|
N int `json:"n"`
|
||||||
|
R int `json:"r"`
|
||||||
|
P int `json:"p"`
|
||||||
|
C int `json:"c"`
|
||||||
|
PRF string `json:"prf"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type cipherparamsJSON struct {
|
type cipherparamsJSON struct {
|
||||||
|
|
|
||||||
|
|
@ -158,12 +158,14 @@ func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error)
|
||||||
}
|
}
|
||||||
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
|
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
|
||||||
|
|
||||||
scryptParamsJSON := make(map[string]interface{}, 5)
|
kdfParamsJson := &kdfParamsJSON{
|
||||||
scryptParamsJSON["n"] = scryptN
|
N: scryptN,
|
||||||
scryptParamsJSON["r"] = scryptR
|
R: scryptR,
|
||||||
scryptParamsJSON["p"] = scryptP
|
P: scryptP,
|
||||||
scryptParamsJSON["dklen"] = scryptDKLen
|
DKlen: scryptDKLen,
|
||||||
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
|
Salt: hex.EncodeToString(salt),
|
||||||
|
}
|
||||||
|
|
||||||
cipherParamsJSON := cipherparamsJSON{
|
cipherParamsJSON := cipherparamsJSON{
|
||||||
IV: hex.EncodeToString(iv),
|
IV: hex.EncodeToString(iv),
|
||||||
}
|
}
|
||||||
|
|
@ -173,7 +175,7 @@ func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error)
|
||||||
CipherText: hex.EncodeToString(cipherText),
|
CipherText: hex.EncodeToString(cipherText),
|
||||||
CipherParams: cipherParamsJSON,
|
CipherParams: cipherParamsJSON,
|
||||||
KDF: keyHeaderKDF,
|
KDF: keyHeaderKDF,
|
||||||
KDFParams: scryptParamsJSON,
|
KDFParams: *kdfParamsJson,
|
||||||
MAC: hex.EncodeToString(mac),
|
MAC: hex.EncodeToString(mac),
|
||||||
}
|
}
|
||||||
return cryptoStruct, nil
|
return cryptoStruct, nil
|
||||||
|
|
@ -332,20 +334,21 @@ func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byt
|
||||||
|
|
||||||
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
|
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
|
||||||
authArray := []byte(auth)
|
authArray := []byte(auth)
|
||||||
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
|
// salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
|
||||||
|
salt, err := hex.DecodeString(cryptoJSON.KDFParams.Salt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
|
dkLen := cryptoJSON.KDFParams.DKlen
|
||||||
|
|
||||||
if cryptoJSON.KDF == keyHeaderKDF {
|
if cryptoJSON.KDF == keyHeaderKDF {
|
||||||
n := ensureInt(cryptoJSON.KDFParams["n"])
|
n := cryptoJSON.KDFParams.N
|
||||||
r := ensureInt(cryptoJSON.KDFParams["r"])
|
r := cryptoJSON.KDFParams.R
|
||||||
p := ensureInt(cryptoJSON.KDFParams["p"])
|
p := cryptoJSON.KDFParams.P
|
||||||
return scrypt.Key(authArray, salt, n, r, p, dkLen)
|
return scrypt.Key(authArray, salt, n, r, p, dkLen)
|
||||||
} else if cryptoJSON.KDF == "pbkdf2" {
|
} else if cryptoJSON.KDF == "pbkdf2" {
|
||||||
c := ensureInt(cryptoJSON.KDFParams["c"])
|
c := cryptoJSON.KDFParams.C
|
||||||
prf := cryptoJSON.KDFParams["prf"].(string)
|
prf := cryptoJSON.KDFParams.PRF
|
||||||
if prf != "hmac-sha256" {
|
if prf != "hmac-sha256" {
|
||||||
return nil, fmt.Errorf("unsupported PBKDF2 PRF: %s", prf)
|
return nil, fmt.Errorf("unsupported PBKDF2 PRF: %s", prf)
|
||||||
}
|
}
|
||||||
|
|
@ -355,14 +358,3 @@ func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
|
||||||
|
|
||||||
return nil, fmt.Errorf("unsupported KDF: %s", cryptoJSON.KDF)
|
return nil, fmt.Errorf("unsupported KDF: %s", cryptoJSON.KDF)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: can we do without this when unmarshalling dynamic JSON?
|
|
||||||
// why do integers in KDF params end up as float64 and not int after
|
|
||||||
// unmarshal?
|
|
||||||
func ensureInt(x interface{}) int {
|
|
||||||
res, ok := x.(int)
|
|
||||||
if !ok {
|
|
||||||
res = int(x.(float64))
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue