JP.
JP.

Reputation: 5594

Writing RSA PrivateKey PEM to file in golang

I'm using spacemonkeygo's openssl library to generate an RSA PrivateKey - the library also offers ways to load a private key from a PEM, but I can't figure out how to turn the private key into an encrypted PEM block - anyone got any ideas?

import "github.com/spacemonkeygo/openssl"

// Generate a private key
privateKey, _ := openssl.GenerateRSAKey(2048)

password := "an encryption password"
// Create an encrypted PEM block from the private key?
pem := ???

// Load the PEM file
loadedPrivateKey, _ := openssl.LoadPrivateKeyFromPEMWidthPassword(pem, password)

privateKey == loadedPrivateKey

Upvotes: 1

Views: 2558

Answers (1)

user4849927
user4849927

Reputation:

Basically you can achieve this with x509.EncryptPEMBlock.

If your key is surrounded by -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- post- and prefixes you might have to remove them before encryption.

Upvotes: 1

Related Questions