为什么我们需要在 Go 中的 Diffie Hellman 密钥交换算法中恢复公钥

问题描述

this 模块文档中(为了简单起见,只考虑 Alice 的一面)示例代码是:

// Get a group. Use the default one would be enough.
g,_ := GetGroup(0)

// Generate a private key from the group.
// Use the default random number generator.
priv,_ := g.GeneratePrivateKey(nil)

// Get the public key from the private key.
pub := priv.Bytes()

// Send the public key to Bob.
Send("Bob",pub)

// Receive a slice of bytes from Bob,which contains Bob's public key
b := Recv("Bob")

// Recover Bob's public key
bobPubKey := NewPublicKey(b)

// Compute the key
k,_ := group.ComputeKey(bobPubKey,priv)

// Get the key in the form of []byte
key := k.Bytes()

这是我的问题:

1)

// Get the public key from the private key.
pub := priv.Bytes()

如何将私有字节用作公钥字节?这只是方法错误命名吗? (应该类似于 priv.GetPubBytes() 假设 priv 包含私钥和公钥)

2)

// Receive a slice of bytes from Bob,which contains Bob's public key
b := Recv("Bob")

// Recover Bob's public key
bobPubKey := NewPublicKey(b)

如果 b 包含 Bob 的公钥(通过频道),那么为什么我们需要恢复它?这个恢复过程将什么转换成什么?

解决方法

  1. 是的,它只是命名。我同意您的评论:更好的名称应该是 .GetPubBytes() 之类的名称或明确表明您从公钥中获取字节的名称。

  2. 再说一次,这就是评论的措辞,没有什么可以从网络中恢复的。

请注意,对于像这样的公共包(托管在 github 上),godoc 页面具有指向代码的直接链接。例如:

  • 如果您向下滚动到 func NewPublicKey (this paragraph) 的文档条目,
  • 点击函数的名称会将您带到 its implementation,on github -- 在这种特定情况下:您可以看到唯一的操作是创建一个 DHKey 结构并将字节分配给它的 y字段,它是密钥的公共部分。