英文:
golang - marshal PKCS8 private key?
问题
在Go 1.5中,有没有一种方法可以将PKCS8私钥进行编组?例如,类似于或从x509.MarshalPKCS1PrivateKey
开始的方法?
英文:
Is there a way to marshal a PKCS8 private key in go 1.5?
e.g. something similar to or starting from x509.MarshalPKCS1PrivateKey
?
答案1
得分: 5
有趣的是,没有标准的函数可以完成这个任务,但是下面是一个自定义的解决方案:
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)
}
希望对你有帮助!
英文:
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)
}
答案2
得分: 4
自从上面的答案写出来以后,Go 1.10已经发布了,其中包含了一个x509.MarshalPKCS8PrivateKey
方法。
英文:
Since the above answer was written, go 1.10 has been released with a x509.MarshalPKCS8PrivateKey
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论