我正在尝试将 ssh.PublicKey 对象转换为 go 服务中的 ssh rsa 字符串,我想通过网络将其发送到其他服务,该服务将使用提供的公钥授权此请求。为此,我需要开放 ssh 格式的确切 rsa 字符串,如下所示在授权服务中
ssh-rsa <pubkey> <comment>
我尝试了这个
fmt.Println(base64.StdEncoding.EncodeToString(key.Marshal()))
,但这会截断评论部分,从而导致授权问题。
在进行相等性检查时我应该忽略身份验证服务中的注释吗?
请帮我解决这个问题。
我认为最好使用
ssh.MarshalAuthorizedKey
来生成所需格式的字符串!在您的授权服务中,当将接收到的公钥与存储的公钥进行比较时,如果与您的授权逻辑无关,您可以忽略注释部分,因为公钥本身应该足以进行比较!
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"golang.org/x/crypto/ssh"
)
func main() {
//generating an RSA key pair
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
publicKey := &privateKey.PublicKey
//converting the public key to an SSH RSA string
authorizedKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
authorizedKeyString := string(authorizedKeyBytes)
fmt.Println(authorizedKeyString)
}