pem

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decoder added in v0.2.0

func Decoder(b []byte, v **stdpem.Block) error

Decoder decodes bytes into a PEM block.

func Encoder added in v0.2.0

func Encoder(v *stdpem.Block) ([]byte, error)

Encoder encodes a PEM block to bytes.

func NewCodec

func NewCodec() encoding.Codec[*stdpem.Block, []byte]

NewCodec returns a PEM codec. It is safe for concurrent use.

Example
package main

import (
	stdpem "encoding/pem"
	"fmt"

	"github.com/foomo/goencode/pem"
)

func main() {
	c := pem.NewCodec()

	block := &stdpem.Block{
		Type:  "TEST",
		Bytes: []byte("hello"),
	}

	encoded, err := c.Encode(block)
	if err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	fmt.Printf("Encoded:\n%s", string(encoded))

	var decoded *stdpem.Block
	if err := c.Decode(encoded, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Type: %s\n", decoded.Type)
	fmt.Printf("Decoded Bytes: %s\n", string(decoded.Bytes))
}
Output:
Encoded:
-----BEGIN TEST-----
aGVsbG8=
-----END TEST-----
Decoded Type: TEST
Decoded Bytes: hello

func NewStreamCodec

func NewStreamCodec() encoding.StreamCodec[*stdpem.Block]

NewStreamCodec returns a PEM stream codec. It is safe for concurrent use.

Example
package main

import (
	"bytes"
	stdpem "encoding/pem"
	"fmt"

	"github.com/foomo/goencode/pem"
)

func main() {
	c := pem.NewStreamCodec()

	block := &stdpem.Block{
		Type:  "TEST",
		Bytes: []byte("hello"),
	}

	var buf bytes.Buffer
	if err := c.Encode(&buf, block); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded *stdpem.Block
	if err := c.Decode(&buf, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Type: %s\n", decoded.Type)
	fmt.Printf("Decoded Bytes: %s\n", string(decoded.Bytes))
}
Output:
Decoded Type: TEST
Decoded Bytes: hello

func StreamDecoder added in v0.2.0

func StreamDecoder(r io.Reader, v **stdpem.Block) error

StreamDecoder decodes a PEM block from a stream.

func StreamEncoder added in v0.2.0

func StreamEncoder(w io.Writer, v *stdpem.Block) error

StreamEncoder encodes a PEM block to a stream.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL