golang – marshal PKCS8 private key?

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

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.

huangapple
  • 本文由 发表于 2015年11月26日 14:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/33932221.html
匿名

发表评论

匿名网友

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

确定