example_test.go raw
1 // Copyright (c) 2014 The btcsuite developers
2 // Copyright (c) 2015-2021 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5
6 // TODO: change this to work with sha256
7
8 package ecdsa_test
9
10 // // This example demonstrates signing a message with a secp256k1 secret key that
11 // // is first parsed from raw bytes and serializing the generated signature.
12 // func ExampleSign() {
13 // // Decode a hex-encoded secret key.
14 // pkBytes, err := hex.Dec("22a47fa09a223f2aa079edf85a7c2d4f87" +
15 // "20ee63e502ee2869afab7de234b80c")
16 // if err != nil {
17 // fmt.Println(err)
18 // return
19 // }
20 // secKey := secp256k1.SecKeyFromBytes(pkBytes)
21 //
22 // // Sign a message using the secret key.
23 // message := "test message"
24 // messageHash := blake256.Sum256(by(message))
25 // signature := ecdsa.Sign(secKey, messageHash[:])
26 //
27 // // Serialize and display the signature.
28 // fmt.Printf("Serialized Signature: %x\n", signature.Serialize())
29 //
30 // // Verify the signature for the message using the public key.
31 // pubKey := secKey.Pubkey()
32 // verified := signature.Verify(messageHash[:], pubKey)
33 // fmt.Printf("Signature Verified? %v\n", verified)
34 //
35 // // Output:
36 // // Serialized Signature: 3045022100fcc0a8768cfbcefcf2cadd7cfb0fb18ed08dd2e2ae84bef1a474a3d351b26f0302200fc1a350b45f46fa00101391302818d748c2b22615511a3ffd5bb638bd777207
37 // // Signature Verified? true
38 // }
39
40 // // This example demonstrates verifying a secp256k1 signature against a public
41 // // key that is first parsed from raw bytes. The signature is also parsed from
42 // // raw bytes.
43 // func ExampleSignature_Verify() {
44 // // Decode hex-encoded serialized public key.
45 // pubKeyBytes, err := hex.Dec("02a673638cb9587cb68ea08dbef685c" +
46 // "6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5")
47 // if err != nil {
48 // fmt.Println(err)
49 // return
50 // }
51 // pubKey, err := secp256k1.ParsePubKey(pubKeyBytes)
52 // if err != nil {
53 // fmt.Println(err)
54 // return
55 // }
56 //
57 // // Decode hex-encoded serialized signature.
58 // sigBytes, err := hex.Dec("3045022100fcc0a8768cfbcefcf2cadd7cfb0" +
59 // "fb18ed08dd2e2ae84bef1a474a3d351b26f0302200fc1a350b45f46fa0010139130" +
60 // "2818d748c2b22615511a3ffd5bb638bd777207")
61 // if err != nil {
62 // fmt.Println(err)
63 // return
64 // }
65 // signature, err := ecdsa.ParseDERSignature(sigBytes)
66 // if err != nil {
67 // fmt.Println(err)
68 // return
69 // }
70 //
71 // // Verify the signature for the message using the public key.
72 // message := "test message"
73 // messageHash := blake256.Sum256(by(message))
74 // verified := signature.Verify(messageHash[:], pubKey)
75 // fmt.Println("Signature Verified?", verified)
76 //
77 // // Output:
78 // // Signature Verified? true
79 // }
80