1. 程式人生 > >golang[35]-區塊鏈-私鑰公鑰生成

golang[35]-區塊鏈-私鑰公鑰生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//生成私鑰和公鑰
func newKeyPair() (ecdsa.PrivateKey,[]byte){

//生成橢圓曲線,  secp256r1 曲線。    比特幣當中的曲線是secp256k1
curve :=elliptic.P256()

private,err :=ecdsa.GenerateKey(curve,rand.Reader)

if err !=nil{

fmt.Println("error")
}
pubkey :=append
(private.PublicKey.X.Bytes(),private.PublicKey.Y.Bytes()...)

return *private,pubkey

}


func main(){

//呼叫函式生成公鑰
privatekey,public :=newKeyPair()

//列印私鑰  曲線上的x點
fmt.Printf("%x\n",privatekey.D.Bytes())

//列印公鑰, 曲線上的x點和y點
fmt.Printf("%x",public)



}

image.png