github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/btcec/example_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package btcec_test 7 8 import ( 9 "encoding/hex" 10 "fmt" 11 12 "github.com/BlockABC/godash/btcec" 13 "github.com/BlockABC/godash/wire" 14 ) 15 16 // This example demonstrates signing a message with a secp256k1 private key that 17 // is first parsed form raw bytes and serializing the generated signature. 18 func Example_signMessage() { 19 // Decode a hex-encoded private key. 20 pkBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2d4f87" + 21 "20ee63e502ee2869afab7de234b80c") 22 if err != nil { 23 fmt.Println(err) 24 return 25 } 26 privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes) 27 28 // Sign a message using the private key. 29 message := "test message" 30 messageHash := wire.DoubleSha256([]byte(message)) 31 signature, err := privKey.Sign(messageHash) 32 if err != nil { 33 fmt.Println(err) 34 return 35 } 36 37 // Serialize and display the signature. 38 fmt.Printf("Serialized Signature: %x\n", signature.Serialize()) 39 40 // Verify the signature for the message using the public key. 41 verified := signature.Verify(messageHash, pubKey) 42 fmt.Printf("Signature Verified? %v\n", verified) 43 44 // Output: 45 // Serialized Signature: 304402201008e236fa8cd0f25df4482dddbb622e8a8b26ef0ba731719458de3ccd93805b022032f8ebe514ba5f672466eba334639282616bb3c2f0ab09998037513d1f9e3d6d 46 // Signature Verified? true 47 } 48 49 // This example demonstrates verifying a secp256k1 signature against a public 50 // key that is first parsed from raw bytes. The signature is also parsed from 51 // raw bytes. 52 func Example_verifySignature() { 53 // Decode hex-encoded serialized public key. 54 pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c" + 55 "6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5") 56 if err != nil { 57 fmt.Println(err) 58 return 59 } 60 pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256()) 61 if err != nil { 62 fmt.Println(err) 63 return 64 } 65 66 // Decode hex-encoded serialized signature. 67 sigBytes, err := hex.DecodeString("30450220090ebfb3690a0ff115bb1b38b" + 68 "8b323a667b7653454f1bccb06d4bbdca42c2079022100ec95778b51e707" + 69 "1cb1205f8bde9af6592fc978b0452dafe599481c46d6b2e479") 70 71 if err != nil { 72 fmt.Println(err) 73 return 74 } 75 signature, err := btcec.ParseSignature(sigBytes, btcec.S256()) 76 if err != nil { 77 fmt.Println(err) 78 return 79 } 80 81 // Verify the signature for the message using the public key. 82 message := "test message" 83 messageHash := wire.DoubleSha256([]byte(message)) 84 verified := signature.Verify(messageHash, pubKey) 85 fmt.Println("Signature Verified?", verified) 86 87 // Output: 88 // Signature Verified? true 89 } 90 91 // This example demonstrates encrypting a message for a public key that is first 92 // parsed from raw bytes, then decrypting it using the corresponding private key. 93 func Example_encryptMessage() { 94 // Decode the hex-encoded pubkey of the recipient. 95 pubKeyBytes, err := hex.DecodeString("04115c42e757b2efb7671c578530ec191a1" + 96 "359381e6a71127a9d37c486fd30dae57e76dc58f693bd7e7010358ce6b165e483a29" + 97 "21010db67ac11b1b51b651953d2") // uncompressed pubkey 98 if err != nil { 99 fmt.Println(err) 100 return 101 } 102 pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256()) 103 if err != nil { 104 fmt.Println(err) 105 return 106 } 107 108 // Encrypt a message decryptable by the private key corresponding to pubKey 109 message := "test message" 110 ciphertext, err := btcec.Encrypt(pubKey, []byte(message)) 111 if err != nil { 112 fmt.Println(err) 113 return 114 } 115 116 // Decode the hex-encoded private key. 117 pkBytes, err := hex.DecodeString("a11b0a4e1a132305652ee7a8eb7848f6ad" + 118 "5ea381e3ce20a2c086a2e388230811") 119 if err != nil { 120 fmt.Println(err) 121 return 122 } 123 // note that we already have corresponding pubKey 124 privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes) 125 126 // Try decrypting and verify if it's the same message. 127 plaintext, err := btcec.Decrypt(privKey, ciphertext) 128 if err != nil { 129 fmt.Println(err) 130 return 131 } 132 133 fmt.Println(string(plaintext)) 134 135 // Output: 136 // test message 137 } 138 139 // This example demonstrates decrypting a message using a private key that is 140 // first parsed from raw bytes. 141 func Example_decryptMessage() { 142 // Decode the hex-encoded private key. 143 pkBytes, err := hex.DecodeString("a11b0a4e1a132305652ee7a8eb7848f6ad" + 144 "5ea381e3ce20a2c086a2e388230811") 145 if err != nil { 146 fmt.Println(err) 147 return 148 } 149 150 privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes) 151 152 ciphertext, err := hex.DecodeString("35f644fbfb208bc71e57684c3c8b437402ca" + 153 "002047a2f1b38aa1a8f1d5121778378414f708fe13ebf7b4a7bb74407288c1958969" + 154 "00207cf4ac6057406e40f79961c973309a892732ae7a74ee96cd89823913b8b8d650" + 155 "a44166dc61ea1c419d47077b748a9c06b8d57af72deb2819d98a9d503efc59fc8307" + 156 "d14174f8b83354fac3ff56075162") 157 158 // Try decrypting the message. 159 plaintext, err := btcec.Decrypt(privKey, ciphertext) 160 if err != nil { 161 fmt.Println(err) 162 return 163 } 164 165 fmt.Println(string(plaintext)) 166 167 // Output: 168 // test message 169 }