Update passphrase.go

This commit is contained in:
iPLAY888 2026-02-15 21:45:07 +03:00 committed by GitHub
parent 0cba803fba
commit a76d90990c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
} }