将公钥编码为OpenSSH格式以供显示

huangapple go评论83阅读模式
英文:

Marshal public key into OpenSSH format for display

问题

我有一个rsa.PublicKey对象(从rsa.PrivateKey中获取)。我想将其导出为OpenSSH格式,以在网页中显示。

我注意到了go.crypto/ssh库,它似乎可以做到这一点。
还有关于它实现的讨论(实际上正是我需要做的)

不幸的是,我遇到了一些困难,因为返回的字节数组是以未知编码的形式,我不能简单地将其转换为字符串来显示。

func PublicKey(rsaKey rsa.PublicKey) string {
  key, _ := ssh.NewPublicKey(&rsaKey)
  marshalled := ssh.MarshalPublicKey(key)
  return string(marshalled)
}

这个函数似乎可以工作,因为它在字符串开头添加了ssh-rsa。然而,大多数字符无法识别。

这是我从lambda公钥中检索到的字节数组:

[0 0 0 7 115 115 104 45 114 115 97 0 0 0 3 1 0 1 0 0 0 65 0 178 153 15 73 196 125 250 140 212 0 174 106 77 27 138 59 106 19 100 43 35 242 139 0 59 251 151 121 10 222 154 76 200 43 139 42 129 116 125 222 192 139 98 150 229 58 8 195 49 104 126 242 92 75 244 147 107 161 192 230 4 30 157 21]

有没有什么提示可以正确地将这个字节数组显示为字符串?

英文:

I have an rsa.PublicKey object (retrieved from an rsa.PrivateKey). And I'm trying to export it into the OpenSSH format, to display it in a web page.

I've noticed the go.crypto/ssh library, which seems to be doing this.
And there's the discussion about it's implementation (it's actually exactly what I need to do)

Unfortunately, I'm getting a bit stuck, as the byte array returned is in an unknown encoding and I can't just transform it to a string to display it.

func PublicKey(rsaKey rsa.PublicKey) string {
  key, _ := ssh.NewPublicKey(&rsaKey)
  marshalled := ssh.MarshalPublicKey(key)
  return string(marshalled)
}

This seems to work as it adds the ssh-rsa at the beginning of the string. However, most characters aren't recognized.

Here's the bytes array I'm retrieving for a lambda public key:
> [0 0 0 7 115 115 104 45 114 115 97 0 0 0 3 1 0 1 0 0 0 65 0 178 153 15 73 196 125 250 140 212 0 174 106 77 27 138 59 106 19 100 43 35 242 139 0 59 251 151 121 10 222 154 76 200 43 139 42 129 116 125 222 192 139 98 150 229 58 8 195 49 104 126 242 92 75 244 147 107 161 192 230 4 30 157 21]

Any hint on properly displaying this bytes array as a string?

答案1

得分: 5

编组密钥是为了传输格式。你只需要对字节进行Base64编码:

base64.StdEncoding.EncodeToString(marshalled) + "\n"
英文:

Marshaling a key is for the wire format. You just need to base64 encode the bytes:

base64.StdEncoding.EncodeToString(marshalled) + "\n"

huangapple
  • 本文由 发表于 2014年2月27日 00:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/22046776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定