77c02f36ee
- Keep comments to 80 cols for consistency with the rest of the code base - Made verify a method off of Signature instead of PublicKey since one verifies a signature with a public key as opposed to the other way around - Return new signature from Sign function directly rather than creating a local temporary variable - Modify a couple of comments as recommended by @owainga - Update sample usage in doc.go for both signing messages and verifying signatures ok @owainga
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package btcec
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/rand"
|
|
"math/big"
|
|
)
|
|
|
|
// PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing
|
|
// things with the the private key without having to directly import the ecdsa
|
|
// package.
|
|
type PrivateKey ecdsa.PrivateKey
|
|
|
|
// PrivKeyFromBytes returns a private and public key for `curve' based on the
|
|
// private key passed as an argument as a byte slice.
|
|
func PrivKeyFromBytes(curve *KoblitzCurve, pk []byte) (*PrivateKey,
|
|
*PublicKey) {
|
|
x, y := curve.ScalarBaseMult(pk)
|
|
|
|
priv := &ecdsa.PrivateKey{
|
|
PublicKey: ecdsa.PublicKey{
|
|
Curve: curve,
|
|
X: x,
|
|
Y: y,
|
|
},
|
|
D: new(big.Int).SetBytes(pk),
|
|
}
|
|
|
|
return (*PrivateKey)(priv), (*PublicKey)(&priv.PublicKey)
|
|
}
|
|
|
|
// ToECDSA returns the private key as a *ecdsa.PrivateKey.
|
|
func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
|
|
return (*ecdsa.PrivateKey)(p)
|
|
}
|
|
|
|
// Sign wraps ecdsa.Sign to sign the provided hash (which should be the result
|
|
// of hashing a larger message) using the private key.
|
|
func (p *PrivateKey) Sign(hash []byte) (*Signature, error) {
|
|
r, s, err := ecdsa.Sign(rand.Reader, p.ToECDSA(), hash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Signature{R: r, S: s}, nil
|
|
}
|