pack

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IVSize = aes.BlockSize

	TimeRFC     = 1         // draft RFC recommended number of rounds
	MemoryRFC   = 64 * 1024 // draft RFC recommended memory cost
	SaltSizeRFC = 16        // draft RFC recommended salt size for password hashing
)

passphrased consts

Variables

View Source
var ErrUnknownTag = errors.New("unknown tag")

ErrUnknownTag is returned when a tag is unknown.

View Source
var ErrUnsupportedCompression = errors.New("unsupported compression algorithm")

ErrUnsupportedCompression is returned when unsupported compression algorithm is used.

Functions

func ArmoredEncoder

func ArmoredEncoder(out io.Writer, blockType string, headers map[string]string) (io.WriteCloser, error)

ArmoredEncoder returns an OpenPGP armored encoder.

func ChainCloser

func ChainCloser(c io.Closer, wc io.WriteCloser) io.WriteCloser

ChainCloser chains the wc to c.

func Compress

func Compress(w io.Writer, c Compressor) (io.WriteCloser, error)

Compress compresses a writer.

func Dearmor added in v0.2.0

func Dearmor(in io.Reader) (string, map[string]string, io.Reader, error)

Dearmor determines if an input is an OpenPGP armored block and decodes it. It does nothing if the input is not an OpenPGP armored block.

func DecodeBinary

func DecodeBinary(r io.Reader, v any) error

DecodeBinary decodes an object from binary MessagePack format.

func DecodeBinaryNew

func DecodeBinaryNew[T any](r io.Reader) (*T, error)

DecodeBinaryNew allocates and decodes an object from binary MessagePack format.

func Decompress

func Decompress(r io.Reader, alg Compression, opts DecompressOpts) (io.Reader, error)

Decompress decompresses a reader.

func Decrypt

func Decrypt(r io.Reader, passphrase string, iv IV, salt []byte, params Argon2Params) *cipher.StreamReader

Decrypt wraps a NewPassphrased stream cipher with cipher.StreamReader.

func DetermineArmor

func DetermineArmor(in io.Reader) (bool, io.Reader, error)

DetermineArmor determines if an input is an OpenPGP armored block. It returns multireader with peeked data.

func EncodeBinary

func EncodeBinary(w io.Writer, v any) error

EncodeBinary encodes an object into binary MessagePack format.

func Encrypt

func Encrypt(w io.Writer, passphrase string, iv IV, salt []byte, params Argon2Params) *cipher.StreamWriter

Encrypt wraps a NewPassphrased stream cipher with cipher.StreamWriter.

func MarshalBinary added in v0.2.0

func MarshalBinary(v any) ([]byte, error)

MarshalBinary encodes an object into binary MessagePack format.

func NewPassphrased

func NewPassphrased(passphrase string, iv IV, salt []byte, params Argon2Params) cipher.Stream

NewPassphrased returns a new stream cipher with derived key. Panics if the passphrase is empty.

func NopCloser

func NopCloser(w io.Writer) io.WriteCloser

NopCloser is analog of io.NopCloser but for io.Writer.

func Pack

func Pack(out io.Writer, v Packable, opts ...Option) error

Pack creates a packet and encodes it into binary format.

func RegisterCompression added in v0.1.3

func RegisterCompression(alg Compression, decompress Decompressor)

RegisterCompression registers a compression algorithm.

func RegisterPacketType

func RegisterPacketType(typ PacketType)

RegisterPacketType registers a packet type.

func UnmarshalBinary added in v0.2.0

func UnmarshalBinary(data []byte, v any) error

UnmarshalBinary decodes an object from binary MessagePack format.

func UnmarshalBinaryNew added in v0.2.0

func UnmarshalBinaryNew[T any](data []byte) (*T, error)

UnmarshalBinaryNew allocates and decodes an object from binary MessagePack format.

func Unpack

func Unpack(in io.Reader, opts ...UnpackOption) (Tag, Packable, error)

Unpack unpacks an object from binary format.

func UnpackExact

func UnpackExact[T Packable](in io.Reader, opts ...UnpackOption) (val T, err error)

UnpackExact decodes an object from binary format and casts it to specified type.

Types

type Argon2Params

type Argon2Params struct {
	Time    uint32 `msgpack:"time"`    // argon2id number of rounds
	Memory  uint32 `msgpack:"memory"`  // argon2id memory cost
	Threads uint8  `msgpack:"threads"` // argon2id parallelism degree
}

Argon2Params contains parameters for argon2id.

func Argon2Defaults

func Argon2Defaults() Argon2Params

Argon2Defaults returns recommended parameters for argon2id.

type ArmoredBlock

type ArmoredBlock = armor.Block

ArmoredBlock represents an OpenPGP armored block.

func DecodeArmored

func DecodeArmored(in io.Reader) (*ArmoredBlock, error)

DecodeArmored decodes an OpenPGP armored block.

type Compression

type Compression byte

Compression represents compression algorithm.

const (
	NoCompression Compression = iota
	CompressionDeflate
	CompressionZstd
	CompressionLz4
)

compression algorithms.

type Compressor added in v0.1.3

type Compressor interface {
	Algorithm() Compression
	Compress(w io.Writer) (io.WriteCloser, error)
}

Compressor represents a compressor.

func Deflate

func Deflate(lvl int) Compressor

Deflate returns a deflate compressor.

func Lz4

func Lz4(lvl int, opts ...Lz4Opts) Compressor

Lz4 returns an lz4 compressor.

func Zstd

func Zstd(lvl int) Compressor

Zstd returns a zstd compressor.

type CustomDecoder

type CustomDecoder = msgpack.CustomDecoder

CustomDecoder is an alias for msgpack.CustomDecoder.

type CustomEncoder

type CustomEncoder = msgpack.CustomEncoder

CustomEncoder is an alias for msgpack.CustomEncoder.

type Decoder

type Decoder = msgpack.Decoder

Decoder is an alias for msgpack.Decoder.

type DecompressOpts added in v0.1.3

type DecompressOpts interface {
	DecompressOpts()
}

DecompressOpts represents decompress options.

type Decompressor added in v0.1.3

type Decompressor func(r io.Reader, opts DecompressOpts) (io.Reader, error)

Decompressor represents a decompressor func.

type Encoder

type Encoder = msgpack.Encoder

Encoder is an alias for msgpack.Encoder.

type Encryption

type Encryption struct {
	IV       IV           `msgpack:"iv"`
	Salt     []byte       `msgpack:"salt"`
	Argon2ID Argon2Params `msgpack:"argon2"`
}

Encryption contains encryption parameters.

type ErrMismatchType added in v0.1.1

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

ErrMismatchType is returned when the object type mismatches the expected one.

func (ErrMismatchType) Error added in v0.1.1

func (e ErrMismatchType) Error() string

type IV

type IV [IVSize]byte

IV is an IV for a passphrased cipher.

type Lz4Opts added in v0.1.3

type Lz4Opts struct {
	// if == 0, runtime.GOMAXPROCS(0) is used
	Threads uint
}

Lz4Opts contains optional parameters for lz4.

func (Lz4Opts) DecompressOpts added in v0.1.3

func (Lz4Opts) DecompressOpts()

DecompressOpts func.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option represents packing option.

func WithCompression

func WithCompression(c Compressor) Option

WithCompression compresses a packet.

func WithEncryption

func WithEncryption(passphrase string, params *Encryption) Option

WithEncryption encrypts a packet. If params is nil, random values are used.

type Packable

type Packable interface {
	PacketTag() Tag
}

Packable represents a packable object.

type Packet

type Packet struct {
	Tag    Tag
	Header struct {
		Encryption  *Encryption `msgpack:"encryption,omitempty"`
		Compression Compression `msgpack:"compression,omitempty"`
	}
	Object Stream
	// contains filtered or unexported fields
}

Packet is a binary packet.

type PacketType

type PacketType struct {
	Tag Tag
	// Must be a settable for the msgpack.
	// A pointer to this type must implement the Packable interface.
	Type      reflect.Type
	Name      string
	BlockType string
}

PacketType represents a binary packet type.

func NewType

func NewType(tag Tag, v Packable, name, blockType string) PacketType

NewType creates a new packet type.

type RawMessage added in v0.2.0

type RawMessage[T any] struct {
	msgpack.RawMessage
}

RawMessage represents a raw msgpack message which can be unpacked into specified type.

func (RawMessage[T]) Unpack added in v0.2.0

func (r RawMessage[T]) Unpack() (*T, error)

type Stream added in v0.2.0

type Stream struct {
	Reader io.Reader
}

Stream represents a msgpack bytes stream. It must be the last field in the message.

func (*Stream) DecodeMsgpack added in v0.2.0

func (s *Stream) DecodeMsgpack(dec *msgpack.Decoder) error

DecodeMsgpack implements msgpack.CustomDecoder.

func (*Stream) EncodeMsgpack added in v0.2.0

func (s *Stream) EncodeMsgpack(enc *msgpack.Encoder) error

EncodeMsgpack implements msgpack.CustomEncoder.

type Tag

type Tag byte

Tag is used to determine the binary packet type.

const TagInvalid Tag = 0x00

TagInvalid is used to indicate an invalid tag.

func (Tag) BlockType

func (t Tag) BlockType() string

BlockType returns the block type of the tag.

func (Tag) String

func (t Tag) String() string

func (Tag) Type

func (t Tag) Type() (PacketType, error)

Type returns the type of the tag.

type UnpackOption

type UnpackOption interface {
	// contains filtered or unexported methods
}

UnpackOption represents unpacking option.

func WithDecompressionOpts

func WithDecompressionOpts(opts map[Compression]DecompressOpts) UnpackOption

WithDecompressionOpts decompresses a packet using provided options.

func WithPassphrase

func WithPassphrase(passphrase func() (string, error)) UnpackOption

WithPassphrase decrypts a packet if it is encrypted. passphrase func called only if the packet is encrypted. Panics if passphrase is nil.

Jump to

Keyboard shortcuts

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