version: 1.10

package pem

import "encoding/pem"

Overview

Package pem implements the PEM data encoding, which originated in Privacy Enhanced Mail. The most common use of PEM encoding today is in TLS keys and certificates. See RFC 1421.

Index

Examples

Package files

pem.go

func Encode

  1. func Encode(out io.Writer, b *Block) error

Encode writes the PEM encoding of b to out.

Example:

  1. block := &pem.Block{
  2. Type: "MESSAGE",
  3. Headers: map[string]string{
  4. "Animal": "Gopher",
  5. },
  6. Bytes: []byte("test"),
  7. }
  8. if err := pem.Encode(os.Stdout, block); err != nil {
  9. log.Fatal(err)
  10. }
  11. // Output:
  12. // -----BEGIN MESSAGE-----
  13. // Animal: Gopher
  14. //
  15. // dGVzdA==
  16. // -----END MESSAGE-----

func EncodeToMemory

  1. func EncodeToMemory(b *Block) []byte

EncodeToMemory returns the PEM encoding of b.

If b has invalid headers and cannot be encoded, EncodeToMemory returns nil. If it is important to report details about this error case, use Encode instead.

type Block

  1. type Block struct {
  2. Type string // The type, taken from the preamble (i.e. "RSA PRIVATE KEY").
  3. Headers map[string]string // Optional headers.
  4. Bytes []byte // The decoded bytes of the contents. Typically a DER encoded ASN.1 structure.
  5. }

A Block represents a PEM encoded structure.

The encoded form is:

  1. -----BEGIN Type-----
  2. Headers
  3. base64-encoded Bytes
  4. -----END Type-----

where Headers is a possibly empty sequence of Key: Value lines.

func Decode

  1. func Decode(data []byte) (p *Block, rest []byte)

Decode will find the next PEM formatted block (certificate, private key etc) in the input. It returns that block and the remainder of the input. If no PEM data is found, p is nil and the whole of the input is returned in rest.

Example:

  1. var pubPEMData = []byte(`
  2. -----BEGIN PUBLIC KEY-----
  3. MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAlRuRnThUjU8/prwYxbty
  4. WPT9pURI3lbsKMiB6Fn/VHOKE13p4D8xgOCADpdRagdT6n4etr9atzDKUSvpMtR3
  5. CP5noNc97WiNCggBjVWhs7szEe8ugyqF23XwpHQ6uV1LKH50m92MbOWfCtjU9p/x
  6. qhNpQQ1AZhqNy5Gevap5k8XzRmjSldNAFZMY7Yv3Gi+nyCwGwpVtBUwhuLzgNFK/
  7. yDtw2WcWmUU7NuC8Q6MWvPebxVtCfVp/iQU6q60yyt6aGOBkhAX0LpKAEhKidixY
  8. nP9PNVBvxgu3XZ4P36gZV6+ummKdBVnc3NqwBLu5+CcdRdusmHPHd5pHf4/38Z3/
  9. 6qU2a/fPvWzceVTEgZ47QjFMTCTmCwNt29cvi7zZeQzjtwQgn4ipN9NibRH/Ax/q
  10. TbIzHfrJ1xa2RteWSdFjwtxi9C20HUkjXSeI4YlzQMH0fPX6KCE7aVePTOnB69I/
  11. a9/q96DiXZajwlpq3wFctrs1oXqBp5DVrCIj8hU2wNgB7LtQ1mCtsYz//heai0K9
  12. PhE4X6hiE0YmeAZjR0uHl8M/5aW9xCoJ72+12kKpWAa0SFRWLy6FejNYCYpkupVJ
  13. yecLk/4L1W0l6jQQZnWErXZYe0PNFcmwGXy1Rep83kfBRNKRy5tvocalLlwXLdUk
  14. AIU+2GKjyT3iMuzZxxFxPFMCAwEAAQ==
  15. -----END PUBLIC KEY-----
  16. and some more`)
  17. block, rest := pem.Decode(pubPEMData)
  18. if block == nil || block.Type != "PUBLIC KEY" {
  19. log.Fatal("failed to decode PEM block containing public key")
  20. }
  21. pub, err := x509.ParsePKIXPublicKey(block.Bytes)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. fmt.Printf("Got a %T, with remaining data: %q", pub, rest)
  26. // Output: Got a *rsa.PublicKey, with remaining data: "and some more"