如何使用GO?
我有以下代码:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
return address
}
现在我想编码publickey和privateKey并保存:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
x509EncodedPri, _ := x509.MarshalECPrivateKey(privKey)
block := pem.Block{
Type: "ECDSA Private Key",
Headers: nil,
Bytes: x509EncodedPri,
}
file, err := os.Create("eccPrivate.pem")
if err != nil {
log.Fatal(err)
}
defer file.Close()
pem.Encode(file, &block)
return address
}
我发现它是空的:
$ cat eccPrivate.pem
-----BEGIN ECDSA Private Key-----
-----END ECDSA Private Key-----
我知道我应该使用ecdsa.generetykey(elliptic.p256(),rand.reader)
>要生成私钥,但我最终得到了与crypto.generekey()
。
不同的地址 如果有人能告诉我我出错了哪里,那将不胜感激。
I have the following code:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
return address
}
Now I want to pem encode the publicKey and privatekey and save:
func newAddress() string {
privKey, _ := crypto.GenerateKey()
publicKey := privKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
x509EncodedPri, _ := x509.MarshalECPrivateKey(privKey)
block := pem.Block{
Type: "ECDSA Private Key",
Headers: nil,
Bytes: x509EncodedPri,
}
file, err := os.Create("eccPrivate.pem")
if err != nil {
log.Fatal(err)
}
defer file.Close()
pem.Encode(file, &block)
return address
}
I found it empty:
$ cat eccPrivate.pem
-----BEGIN ECDSA Private Key-----
-----END ECDSA Private Key-----
I know I should use ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
to generate the private key, but I end up with a different address than crypto.GenerateKey()
.
If anyone can tell me where I am going wrong, it would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论