bytesx

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package bytesx provides functions operating on bytes.

Specifically we implement these operations:

1. generating random bytes;

2. OpenVPN options encoding and decoding;

3. PKCS#7 padding and unpadding.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEncodeOption indicates an option encoding error occurred.
	ErrEncodeOption = errors.New("can't encode option")

	// ErrDecodeOption indicates an option decoding error occurred.
	ErrDecodeOption = errors.New("can't decode option")

	// ErrPaddingPKCS7 indicates that a PKCS#7 padding error has occurred.
	ErrPaddingPKCS7 = errors.New("PKCS#7 padding error")

	// ErrUnpaddingPKCS7 indicates that a PKCS#7 unpadding error has occurred.
	ErrUnpaddingPKCS7 = errors.New("PKCS#7 unpadding error")
)

Functions

func BytesPadPKCS7

func BytesPadPKCS7(b []byte, blockSize int) ([]byte, error)

bytesPadPKCS7 returns the PKCS#7 padding of a byte array.

func BytesUnpadPKCS7

func BytesUnpadPKCS7(b []byte, blockSize int) ([]byte, error)

BytesUnpadPKCS7 performs the PKCS#7 unpadding of a byte array.

func DecodeOptionStringFromBytes

func DecodeOptionStringFromBytes(b []byte) (string, error)

DecodeOptionStringFromBytes returns the string-value for the null-terminated string returned by the server when sending remote options to us.

This function returns errDecodeOption on failure.

func EncodeOptionStringToBytes

func EncodeOptionStringToBytes(s string) ([]byte, error)

EncodeOptionStringToBytes is used to encode the options string, username and password.

According to the OpenVPN protocol, options are represented as a two-byte word, plus the byte representation of the string, null-terminated.

See https://openvpn.net/community-resources/openvpn-protocol/.

This function returns errEncodeOption in case of failure.

func GenRandomBytes

func GenRandomBytes(size int) ([]byte, error)

genRandomBytes returns an array of bytes with the given size using a CSRNG, on success, or an error, in case of failure.

func ReadUint32

func ReadUint32(buf *bytes.Buffer) (uint32, error)

ReadUint32 is a convenience function that reads a uint32 from a 4-byte buffer, returning an error if the operation failed.

func WriteUint24

func WriteUint24(buf *bytes.Buffer, val uint32)

WriteUint24 is a convenience function that appends to the given buffer 3 bytes containing the big-endian representation of the given uint32 value. Caller is responsible to ensure the passed value does not overflow the maximal capacity of 3 bytes.

func WriteUint32

func WriteUint32(buf *bytes.Buffer, val uint32)

WriteUint32 is a convenience function that appends to the given buffer 4 bytes containing the big-endian representation of the given uint32 value. Caller is responsible to ensure the passed value does not overflow the maximal capacity of 4 bytes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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