Home >Backend Development >Golang >How Can I Marshal PKCS8 Private Keys in Go 1.5?
Marshalling PKCS8 Private Keys in Go
The question arises regarding the availability of a method to marshal PKCS8 private keys in Go 1.5. A similar function, such as x509.MarshalPKCS1PrivateKey, is sought after.
Custom Solution
Interestingly, there is no standard function for marshalling PKCS8 private keys in Go. However, a custom solution is provided below:
<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>
This custom function, rsa2pkcs8, can be used to marshal PKCS8 private keys in Go 1.5.
The above is the detailed content of How Can I Marshal PKCS8 Private Keys in Go 1.5?. For more information, please follow other related articles on the PHP Chinese website!