example_test.go raw

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