从私有 ED25519 Go 获取 public 密钥

Get public key from private ED25519 Go

我正在尝试使用带 Go 的 ED25519 从私钥中提取 public 密钥。

我将我的私钥字节值传递到我的方法中,从中创建一个新的 ed25519 私钥结构,然后使用 .Public() 方法检索 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")
}

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

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 私钥和 public 密钥 连接:64 字节,在本包中称为“私钥

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

如果你只有一个由RFC 8032组成的32字节切片<private key>,你需要计算public键如下:

// 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