[英]golang - marshal PKCS8 private key?
Is there a way to marshal a PKCS8 private key in go 1.5?有没有办法在 go 1.5 中编组 PKCS8 私钥?
eg something similar to or starting from x509.MarshalPKCS1PrivateKey
?例如类似于或从
x509.MarshalPKCS1PrivateKey
开始的x509.MarshalPKCS1PrivateKey
?
Funny enough, there are no standard function to do that, but here is a custom solution:有趣的是,没有标准功能可以做到这一点,但这里有一个自定义解决方案:
type pkcs8Key struct {
Version int
PrivateKeyAlgorithm []asn1.ObjectIdentifier
PrivateKey []byte
}
func rsa2pkcs8(key *rsa.PrivateKey) ([]byte, error) {
var pkey pkcs8Key
pkey.Version = 0
pkey.PrivateKeyAlgorithm = make([]asn1.ObjectIdentifier, 1)
pkey.PrivateKeyAlgorithm[0] = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
pkey.PrivateKey = x509.MarshalPKCS1PrivateKey(key)
return asn1.Marshal(pkey)
}
自从写了上面的答案以来,go 1.10 已经发布了一个x509.MarshalPKCS8PrivateKey
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.