cbor

package
v0.2202.5 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: Apache-2.0 Imports: 7 Imported by: 77

Documentation

Overview

Package cbor provides helpers for encoding and decoding canonical CBOR.

Using this package will produce canonical encodings which can be used in cryptographic contexts like signing as the same message is guaranteed to always have the same serialization.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidVersion is the error returned when a versioned
	// serialized blob is either missing, or has an invalid version.
	ErrInvalidVersion = errors.New("cbor: missing or invalid version")
)

Functions

func FixSliceForSerde

func FixSliceForSerde(b []byte) []byte

FixSliceForSerde will convert `nil` to `[]byte` to work around serde brain damage.

func GetVersion

func GetVersion(data []byte) (uint16, error)

GetVersion returns the version of a versioned serializable data structure, if any.

func Marshal

func Marshal(src interface{}) []byte

Marshal serializes a given type into a CBOR byte vector.

func MustUnmarshal

func MustUnmarshal(data []byte, dst interface{})

MustUnmarshal deserializes a CBOR byte vector into a given type. Panics if unmarshal fails.

func NewDecoder

func NewDecoder(r io.Reader) *cbor.Decoder

NewDecoder creates a new CBOR decoder.

func NewEncoder

func NewEncoder(w io.Writer) *cbor.Encoder

NewEncoder creates a new CBOR encoder.

func Unmarshal

func Unmarshal(data []byte, dst interface{}) error

Unmarshal deserializes a CBOR byte vector into a given type.

func UnmarshalTrusted

func UnmarshalTrusted(data []byte, dst interface{}) error

UnmarshalTrusted deserializes a CBOR byte vector into a given type.

This method MUST ONLY BE USED FOR TRUSTED INPUTS as it relaxes some decoding restrictions.

Types

type Marshaler added in v0.2100.0

type Marshaler = cbor.Marshaler

Marshaler is the interface implemented by types that can marshal themselves into valid CBOR.

type MessageCodec

type MessageCodec struct {
	MessageReader
	MessageWriter
}

MessageCodec is a length-prefixed Message encoder/decoder.

func NewMessageCodec

func NewMessageCodec(rw io.ReadWriter, module string) *MessageCodec

NewMessageCodec constructs a new Message encoder/decoder.

type MessageReader

type MessageReader struct {
	// contains filtered or unexported fields
}

MessageReader is a reader wrapper that decodes CBOR-encoded Message structures.

func (*MessageReader) Read

func (c *MessageReader) Read(msg interface{}) error

Read deserializes a single CBOR-encoded Message from the underlying reader.

type MessageWriter

type MessageWriter struct {
	// contains filtered or unexported fields
}

MessageWriter is a writer wrapper that encodes Messages structures to CBOR.

func (*MessageWriter) Write

func (c *MessageWriter) Write(msg interface{}) error

Write serializes a single Message to CBOR and writes it to the underlying writer.

type RawMessage

type RawMessage = cbor.RawMessage

RawMessage is a raw encoded CBOR value. It implements Marshaler and Unmarshaler interfaces and can be used to delay CBOR decoding or precompute a CBOR encoding.

type Unmarshaler added in v0.2100.0

type Unmarshaler = cbor.Unmarshaler

Unmarshaler is the interface implemented by types that wish to unmarshal CBOR data themselves. The input is a valid CBOR value. UnmarshalCBOR must copy the CBOR data if it needs to use it after returning.

type Versioned

type Versioned struct {
	V uint16 `json:"v"`
}

Versioned is a generic versioned serializable data structure.

func NewVersioned

func NewVersioned(v uint16) Versioned

NewVersioned creates a new Versioned structure with the specified version.

Jump to

Keyboard shortcuts

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