github.com/insolar/x-crypto@v0.0.0-20191031140942-75fab8a325f6/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  	"fmt"
     9  	"github.com/insolar/x-crypto/ecdsa"
    10  	"github.com/insolar/x-crypto/elliptic"
    11  	"github.com/insolar/x-crypto/rand"
    12  	"github.com/insolar/x-crypto/sha256"
    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  }