Use crypto/ecdsa to generate ECDSA key pair

This commit is contained in:
Ivan Kuznetsov 2017-09-07 17:05:57 +07:00
parent 5a1e6f7e47
commit caf71744f5
1 changed files with 9 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
@ -19,13 +20,14 @@ const walletFile = "wallet.dat"
// Wallet ... // Wallet ...
type Wallet struct { type Wallet struct {
PrivateKey []byte PrivateKey ecdsa.PrivateKey
PublicKey []byte PublicKey ecdsa.PublicKey
} }
// GetAddress returns wallet address // GetAddress returns wallet address
func (w Wallet) GetAddress() []byte { func (w Wallet) GetAddress() []byte {
publicSHA256 := sha256.Sum256(w.PublicKey) public := append(w.PublicKey.X.Bytes(), w.PublicKey.Y.Bytes()...)
publicSHA256 := sha256.Sum256(public)
RIPEMD160Hasher := ripemd160.New() RIPEMD160Hasher := ripemd160.New()
_, err := RIPEMD160Hasher.Write(publicSHA256[:]) _, err := RIPEMD160Hasher.Write(publicSHA256[:])
@ -47,6 +49,7 @@ func (w Wallet) GetAddress() []byte {
func (w Wallet) SaveToFile() { func (w Wallet) SaveToFile() {
var content bytes.Buffer var content bytes.Buffer
gob.Register(w.PrivateKey.Curve)
encoder := gob.NewEncoder(&content) encoder := gob.NewEncoder(&content)
err := encoder.Encode(w) err := encoder.Encode(w)
if err != nil { if err != nil {
@ -70,16 +73,14 @@ func NewWallet() (*Wallet, error) {
return &wallet, nil return &wallet, nil
} }
func newKeyPair() ([]byte, []byte) { func newKeyPair() (ecdsa.PrivateKey, ecdsa.PublicKey) {
curve := elliptic.P256() curve := elliptic.P256()
private, x, y, err := elliptic.GenerateKey(curve, rand.Reader) private, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
public := append(x.Bytes(), y.Bytes()...) return *private, private.PublicKey
return private, public
} }
// Checksum ... // Checksum ...