首頁  >  文章  >  後端開發  >  如何在 Go 1.5 封送 PKCS8 私鑰?

如何在 Go 1.5 封送 PKCS8 私鑰?

DDD
DDD原創
2024-10-26 16:03:30207瀏覽

How Can I Marshal PKCS8 Private Keys in Go 1.5?

在 Go 中編組 PKCS8 私鑰

出現了關於在 Go 1.5 中編組 PKCS8 私鑰的方法的可用性的問題。類似的函數,例如 x509.MarshalPKCS1PrivateKey,受到追捧。

自訂解決方案

有趣的是,Go 中沒有用於編組 PKCS8 私鑰的標準函數。不過,以下提供了一個自訂解決方案:

<code class="go">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)
}</code>

這個自訂函數 rsa2pkcs8 可用於在 Go 1.5 中編組 PKCS8 私鑰。

以上是如何在 Go 1.5 封送 PKCS8 私鑰?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn