github.com/FISCO-BCOS/crypto@v0.0.0-20200202032121-bd8ab0b5d4f1/ecdsa/example_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ecdsa_test
     6  
     7  import (
     8  	"github.com/FISCO-BCOS/crypto/ecdsa"
     9  	"github.com/FISCO-BCOS/crypto/elliptic"
    10  	"crypto/rand"
    11  	"crypto/sha256"
    12  	"fmt"
    13  )
    14  
    15  func Example() {
    16  	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	msg := "hello, world"
    22  	hash := sha256.Sum256([]byte(msg))
    23  
    24  	r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:])
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	fmt.Printf("signature: (0x%x, 0x%x)\n", r, s)
    29  
    30  	valid := ecdsa.Verify(&privateKey.PublicKey, hash[:], r, s)
    31  	fmt.Println("signature verified:", valid)
    32  }