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

猜你喜欢

转载自blog.51cto.com/13784902/2329702