Skip to content

Commit db810b6

Browse files
philpearlFiloSottile
authored andcommitted
crypto/ecdsa: add a package level example
Change-Id: I4063d5ec4ac45561b94472b528583be564981912 Reviewed-on: https://go-review.googlesource.com/120144 Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 25aaeaa commit db810b6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/crypto/ecdsa/example_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
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+
}

0 commit comments

Comments
 (0)