Source file
src/crypto/ecdsa/example_test.go
1
2
3
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 }
33
View as plain text