diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-06-01 15:24:40 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-06-01 16:11:06 +0800 |
commit | 72dd51e25a5c1553a5a7fc5f0fb84fbe3546682f (patch) | |
tree | 69e7f8440b391b306044243e1032918a18993112 /crypto/crypto.go | |
parent | 067dc2cbf5121541aea8c6089ac42ce07582ead1 (diff) | |
download | dexon-72dd51e25a5c1553a5a7fc5f0fb84fbe3546682f.tar.gz dexon-72dd51e25a5c1553a5a7fc5f0fb84fbe3546682f.tar.zst dexon-72dd51e25a5c1553a5a7fc5f0fb84fbe3546682f.zip |
accounts/keystore, crypto: don't enforce key checks on existing keyfiles
Diffstat (limited to 'crypto/crypto.go')
-rw-r--r-- | crypto/crypto.go | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go index d38ffd0d5..8161769d3 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -68,9 +68,6 @@ func Keccak512(data ...[]byte) []byte { return d.Sum(nil) } -// Deprecated: For backward compatibility as other packages depend on these -func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) } - // Creates an ethereum address given the bytes and the nonce func CreateAddress(b common.Address, nonce uint64) common.Address { data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) @@ -79,9 +76,24 @@ func CreateAddress(b common.Address, nonce uint64) common.Address { // ToECDSA creates a private key with the given D value. func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) { + return toECDSA(d, true) +} + +// ToECDSAUnsafe blidly converts a binary blob to a private key. It should almost +// never be used unless you are sure the input is valid and want to avoid hitting +// errors due to bad origin encoding (0 prefixes cut off). +func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey { + priv, _ := toECDSA(d, false) + return priv +} + +// toECDSA creates a private key with the given D value. The strict parameter +// controls whether the key's length should be enforced at the curve size or +// it can also accept legacy encodings (0 prefixes). +func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) { priv := new(ecdsa.PrivateKey) priv.PublicKey.Curve = S256() - if 8*len(d) != priv.Params().BitSize { + if strict && 8*len(d) != priv.Params().BitSize { return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize) } priv.D = new(big.Int).SetBytes(d) @@ -89,11 +101,12 @@ func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) { return priv, nil } -func FromECDSA(prv *ecdsa.PrivateKey) []byte { - if prv == nil { +// FromECDSA exports a private key into a binary dump. +func FromECDSA(priv *ecdsa.PrivateKey) []byte { + if priv == nil { return nil } - return math.PaddedBigBytes(prv.D, 32) + return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) } func ToECDSAPub(pub []byte) *ecdsa.PublicKey { @@ -121,7 +134,6 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) { } // LoadECDSA loads a secp256k1 private key from the given file. -// The key data is expected to be hex-encoded. func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { buf := make([]byte, 64) fd, err := os.Open(file) |