pem

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec struct{}

Codec is a Codec[*pem.Block] backed by encoding/pem. 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 NewCodec

func NewCodec() *Codec

NewCodec returns a PEM serializer.

func (Codec) Decode

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

func (Codec) Encode

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

type StreamCodec

type StreamCodec struct{}

StreamCodec is a StreamCodec[*pem.Block] backed by encoding/pem. 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 NewStreamCodec

func NewStreamCodec() *StreamCodec

NewStreamCodec returns a PEM stream serializer.

func (StreamCodec) Decode

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

func (StreamCodec) Encode

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

Jump to

Keyboard shortcuts

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