Golang crypto/x509.EncryptPEMBlock function example

package crypto/x509

EncryptPEMBlock returns a PEM block of the specified type holding the given DER-encoded data encrypted with the specified algorithm and password.

Golang crypto/x509.EncryptPEMBlock function usage example

 blockType := "RSA PRIVATE KEY"
 password := []byte("password")


 // see http://golang.org/pkg/crypto/x509/#pkg-constants
 cipherType := x509.PEMCipherAES256

 EncryptedPEMBlock, err := x509.EncryptPEMBlock(rand.Reader,
 blockType,
 []byte("secret message"),
 password,
 cipherType)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

See https://www.socketloop.com/tutorials/golang-encrypt-and-decrypt-data-with-x509-crypto

Reference :

http://golang.org/pkg/crypto/x509/#EncryptPEMBlock

  See also : Golang crypto/x509.DecryptPEMBlock function example

Advertisement