slice

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Unlicense Imports: 8 Imported by: 0

Documentation

Overview

Package slice is a collection of miscellaneous functions involving slices of bytes, including little-endian encoding for 16, 32 and 64-bit unsigned integers used for serialisation length prefixes and system entropy based hash chain padding.

Index

Constants

View Source
const (
	Uint64Len = 8
	Uint32Len = 4
	Uint24Len = 3
	Uint16Len = 2
)

Variables

This section is empty.

Functions

func Cat

func Cat(chunks ...[]byte) (pkt []byte)

Cat takes a slice of byte slices and packs them together in a packet. The returned packet has its capacity pre-allocated to match what gets copied into it by append.

func Cut

func Cut(b []byte, l int) (seg []byte, rem []byte)

func DecodeUint16

func DecodeUint16(b []byte) int

DecodeUint16 returns an int containing the little endian encoded 32bit value stored in a 4 byte long slice

func DecodeUint24

func DecodeUint24(b []byte) int

DecodeUint24 returns an int containing the little endian encoded 24bit value stored in a 3 byte long slice

func DecodeUint32

func DecodeUint32(b []byte) int

DecodeUint32 returns an int containing the little endian encoded 32bit value stored in a 4 byte long slice

func DecodeUint64

func DecodeUint64(b []byte) uint64

DecodeUint64 returns an int containing the little endian encoded 64-bit value stored in a 4 byte long slice

func EncodeUint16

func EncodeUint16(b []byte, n int)

EncodeUint16 puts an int into a uint32 and then into 2 byte long slice.

func EncodeUint24

func EncodeUint24(b []byte, n int)

EncodeUint24 puts an int into a uint32 and then into 3 byte long slice.

func EncodeUint32

func EncodeUint32(b []byte, n int)

EncodeUint32 puts an int into a uint32 and then into 4 byte long slice.

func EncodeUint64

func EncodeUint64(b []byte, n uint64)

EncodeUint64 puts an int into a uint32 and then into 8 byte long slice.

func GenerateRandomAddrPortIPv4

func GenerateRandomAddrPortIPv4() (ap *netip.AddrPort)

func GenerateRandomAddrPortIPv6

func GenerateRandomAddrPortIPv6() (ap *netip.AddrPort)

func NoisePad

func NoisePad(l int) (noise []byte)

func Segment

func Segment(b []byte, segmentSize int) (segs [][]byte)

func SumLen

func SumLen(chunks ...[]byte) (l int)

Types

type Bytes

type Bytes []byte

func NewBytes added in v0.1.13

func NewBytes(length int) Bytes

func NewUint16

func NewUint16() Bytes

func NewUint24

func NewUint24() Bytes

func NewUint32

func NewUint32() Bytes

func NewUint64

func NewUint64() Bytes

func ToBytes

func ToBytes(b []byte) (msg Bytes)

func (Bytes) Len

func (b Bytes) Len() int

func (Bytes) String

func (b Bytes) String() string

func (Bytes) ToBytes

func (b Bytes) ToBytes() []byte

func (Bytes) ToU64Slice

func (b Bytes) ToU64Slice() (u U64Slice)

ToU64Slice converts the message with zero allocations if the slice capacity was already 8 plus the modulus of the length and 8, otherwise this function will trigger a stack allocation, or heap, if the bytes are very long. This is intended to be used with short byte slices like cipher nonces and hashes, so it usually won't trigger allocations off stack and very often won't trigger a copy on stack, saving a lot of time in a short, oft repeated operations.

func (Bytes) Zero

func (b Bytes) Zero()

type Cursor

type Cursor int

func NewCursor

func NewCursor() (c *Cursor)

func (*Cursor) Inc

func (c *Cursor) Inc(v int) Cursor

type U64Slice

type U64Slice []uint64

func (U64Slice) Copy

func (u U64Slice) Copy() (o U64Slice)

func (U64Slice) ToMessage

func (u U64Slice) ToMessage() (m Bytes)

func (U64Slice) XOR

func (u U64Slice) XOR(v U64Slice)

XOR the U64Slice with the provided slice. Panics if slices are different length. The receiver value is mutated in this operation.

func (U64Slice) Zero

func (u U64Slice) Zero()

Jump to

Keyboard shortcuts

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