golang - marshal PKCS8 private key?

[亡魂溺海] 提交于 2019-12-01 03:34:04

问题


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:


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)
}


来源:https://stackoverflow.com/questions/33932221/golang-marshal-pkcs8-private-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!