Added length checkes

This commit is contained in:
obscuren 2014-12-12 22:24:04 +01:00
parent 1c7fd62e57
commit 06e76422b5
1 changed files with 18 additions and 1 deletions

View File

@ -56,6 +56,10 @@ func Ecrecover(data []byte) []byte {
// New methods using proper ecdsa keys from the stdlib // New methods using proper ecdsa keys from the stdlib
func ToECDSA(prv []byte) *ecdsa.PrivateKey { func ToECDSA(prv []byte) *ecdsa.PrivateKey {
if len(prv) == 0 {
return nil
}
priv := new(ecdsa.PrivateKey) priv := new(ecdsa.PrivateKey)
priv.PublicKey.Curve = S256() priv.PublicKey.Curve = S256()
priv.D = ethutil.BigD(prv) priv.D = ethutil.BigD(prv)
@ -64,14 +68,27 @@ func ToECDSA(prv []byte) *ecdsa.PrivateKey {
} }
func FromECDSA(prv *ecdsa.PrivateKey) []byte { func FromECDSA(prv *ecdsa.PrivateKey) []byte {
if prv == nil {
return nil
}
return prv.D.Bytes() return prv.D.Bytes()
} }
func PubToECDSA(pub []byte) *ecdsa.PublicKey { func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
if len(pub) == 0 {
return nil
}
x, y := elliptic.Unmarshal(S256(), pub) x, y := elliptic.Unmarshal(S256(), pub)
return &ecdsa.PublicKey{S256(), x, y} return &ecdsa.PublicKey{S256(), x, y}
} }
func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
if pub == nil {
return nil
}
return elliptic.Marshal(S256(), pub.X, pub.Y)
}
func GenerateKey() (*ecdsa.PrivateKey, error) { func GenerateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(S256(), rand.Reader) return ecdsa.GenerateKey(S256(), rand.Reader)
} }