github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/btcutilecc/examples/ecdh/ecdh.go (about)

     1  // Copyright Turing Corp. 2018 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 main
     6  
     7  import (
     8  	"crypto/rand"
     9  	"fmt"
    10  
    11  	btcutil "github.com/turingchain2020/turingchain/wallet/bipwallet/btcutilecc"
    12  )
    13  
    14  func main() {
    15  	// generate keys for Alice and Bob
    16  	alice, err := btcutil.GenerateKey(rand.Reader)
    17  	if err != nil {
    18  		fmt.Println("Generate alice Key err", err)
    19  	}
    20  	bob, err := btcutil.GenerateKey(rand.Reader)
    21  	if err != nil {
    22  		fmt.Println("Generate bob Key err", err)
    23  	}
    24  	fmt.Printf("Alice:\t%x\n\t%x\n", alice.D, alice.PublicKey.X)
    25  	fmt.Printf("Bob:\t%x\n\t%x\n", bob.D, bob.PublicKey.X)
    26  	fmt.Println("")
    27  
    28  	// Alice calculates shared secret
    29  	aliceShared := btcutil.ECDH(alice, &bob.PublicKey)
    30  	fmt.Printf("Alice: %x\n", aliceShared)
    31  
    32  	// Bob calculates shared secret
    33  	bobShared := btcutil.ECDH(bob, &alice.PublicKey)
    34  	fmt.Printf("Bob:   %x\n", bobShared)
    35  }