| 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 |
"crypto/ecdsa" |
| 9 |
"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 |
sig, err := ecdsa.SignASN1(rand.Reader, privateKey, hash[:]) |
| 25 |
if err != nil { |
| 26 |
panic(err) |
| 27 |
} |
| 28 |
fmt.Printf("signature: %x\n", sig) |
| 29 |
|
| 30 |
valid := ecdsa.VerifyASN1(&privateKey.PublicKey, hash[:], sig) |
| 31 |
fmt.Println("signature verified:", valid) |
| 32 |
} |