首頁 > 後端開發 > Golang > 主體

如何在 Go 1.5 編組 PKCS8 私鑰?

Mary-Kate Olsen
發布: 2024-10-26 16:41:30
原創
349 人瀏覽過

 How to Marshal PKCS8 Private Keys in Go 1.5?

在 Go 中編組 PKCS8 私鑰

可以在 Go 1.5 編組 PKCS8 私鑰嗎?具體來說,是否有類似 PKCS8 的 x509.MarshalPKCS1PrivateKey 的方法?

解決方案:

儘管缺少用於此任務的標準函數,但可以使用自訂解決方案:

<code class="go">import (
    "crypto/rsa"
    "crypto/asn1"
    "crypto/x509"
)

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>
登入後複製

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

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!