mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 03:10:48 +00:00
Update passphrase.go
This commit is contained in:
parent
0cba803fba
commit
a76d90990c
1 changed files with 39 additions and 15 deletions
|
|
@ -332,20 +332,42 @@ 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))
|
saltStr, ok := cryptoJSON.KDFParams["salt"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("invalid KDF params: salt is not a string")
|
||||||
|
}
|
||||||
|
salt, err := hex.DecodeString(saltStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dkLen, err := ensureInt(cryptoJSON.KDFParams["dklen"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
|
|
||||||
|
|
||||||
if cryptoJSON.KDF == keyHeaderKDF {
|
if cryptoJSON.KDF == keyHeaderKDF {
|
||||||
n := ensureInt(cryptoJSON.KDFParams["n"])
|
n, err := ensureInt(cryptoJSON.KDFParams["n"])
|
||||||
r := ensureInt(cryptoJSON.KDFParams["r"])
|
if err != nil {
|
||||||
p := ensureInt(cryptoJSON.KDFParams["p"])
|
return nil, err
|
||||||
|
}
|
||||||
|
r, err := ensureInt(cryptoJSON.KDFParams["r"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p, err := ensureInt(cryptoJSON.KDFParams["p"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
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, err := ensureInt(cryptoJSON.KDFParams["c"])
|
||||||
prf := cryptoJSON.KDFParams["prf"].(string)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
prf, ok := cryptoJSON.KDFParams["prf"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("invalid KDF params: prf is not a string")
|
||||||
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
@ -356,13 +378,15 @@ 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?
|
// ensureInt extracts an integer from a JSON-decoded interface{} value.
|
||||||
// why do integers in KDF params end up as float64 and not int after
|
// JSON numbers are unmarshalled as float64 when decoded into interface{}.
|
||||||
// unmarshal?
|
func ensureInt(x interface{}) (int, error) {
|
||||||
func ensureInt(x interface{}) int {
|
switch v := x.(type) {
|
||||||
res, ok := x.(int)
|
case int:
|
||||||
if !ok {
|
return v, nil
|
||||||
res = int(x.(float64))
|
case float64:
|
||||||
|
return int(v), nil
|
||||||
|
default:
|
||||||
|
return 0, fmt.Errorf("invalid KDF parameter: expected number, got %T", x)
|
||||||
}
|
}
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue