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 ¶
- Variables
- func BytesPadPKCS7(b []byte, blockSize int) ([]byte, error)
- func BytesUnpadPKCS7(b []byte, blockSize int) ([]byte, error)
- func DecodeOptionStringFromBytes(b []byte) (string, error)
- func EncodeOptionStringToBytes(s string) ([]byte, error)
- func GenRandomBytes(size int) ([]byte, error)
- func ReadUint32(buf *bytes.Buffer) (uint32, error)
- func WriteUint24(buf *bytes.Buffer, val uint32)
- func WriteUint32(buf *bytes.Buffer, val uint32)
Constants ¶
This section is empty.
Variables ¶
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 ¶
bytesPadPKCS7 returns the PKCS#7 padding of a byte array.
func BytesUnpadPKCS7 ¶
BytesUnpadPKCS7 performs the PKCS#7 unpadding of a byte array.
func DecodeOptionStringFromBytes ¶
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 ¶
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 ¶
genRandomBytes returns an array of bytes with the given size using a CSRNG, on success, or an error, in case of failure.
func ReadUint32 ¶
ReadUint32 is a convenience function that reads a uint32 from a 4-byte buffer, returning an error if the operation failed.
func WriteUint24 ¶
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 ¶
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.