github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/crypto/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  // +build gccgo_examples
     6  
     7  package ecdsa_test
     8  
     9  import (
    10  	"crypto/ecdsa"
    11  	"crypto/elliptic"
    12  	"crypto/rand"
    13  	"crypto/sha256"
    14  	"fmt"
    15  )
    16  
    17  func Example() {
    18  	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	msg := "hello, world"
    24  	hash := sha256.Sum256([]byte(msg))
    25  
    26  	r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:])
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  	fmt.Printf("signature: (0x%x, 0x%x)\n", r, s)
    31  
    32  	valid := ecdsa.Verify(&privateKey.PublicKey, hash[:], r, s)
    33  	fmt.Println("signature verified:", valid)
    34  }