gpt4 book ai didi

go - 从私有(private) ED25519 Go 获取公钥

转载 作者:行者123 更新时间:2023-12-01 22:13:56 34 4
gpt4 key购买 nike

我正在尝试使用 ED25519 和 Go 从私钥中提取公钥。

我将我的私钥字节值传递到我的方法中,从中创建一个新的 ed25519 私钥结构,然后使用 .Public() 方法检索公钥。

pk := ed25519.PrivateKey(privateKey).Public()
cpk, ok := pk.(ed25519.PublicKey)
if !ok {
return nil, errors.New("problem casting public key to ed25519 public key")
}

这不是错误,但生成的公钥字节始终为空,是不是我在创建私钥结构时做错了什么?

最佳答案

ed25519包在概述中有这条重要评论:

... unlike RFC 8032's formulation, this package's private key representation includes a public key suffix to make multiple signing operations with the same key more efficient. This package refers to the RFC 8032 private key as the “seed”.

这意味着它使用了以下等价物和术语:

  • RFC 8032私钥:32字节,在本包中称为“种子
  • RFC 8032 private key and public key concateneed:64 bytes,在本包中称为“private key

如果您已经有一个由 RFC 8032 <private key><public key> 组成的 64 字节 slice ,您可以使用问题中的代码。

如果您只有一个由 RFC 8032 <private key> 组成的 32 字节 slice ,您需要按如下方式计算公钥:

// Compute the full 64 byte <private key><public key> from the private key
priv := ed25519.NewKeyFromSeed(32bytePrivateKey)

// Print out the public key (the last 32 bytes)
fmt.Println(priv.Public())

请注意:ed25519.PrivateKeyed25519.PublicKey都是type []byte .

关于go - 从私有(private) ED25519 Go 获取公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61778516/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com