Bytearray Operations

Module Functionality
Provides bytearray handling including: conversion between raw bytes and hex-encoded strings, reversing the endianness of a bytearray, and a constant-time method of comparing buffers.


bool cryptx_bytes_compare(const void *buf1, const void *buf2, size_t len)

Compare two bytearrays using a constant-time algorithm.

  • buf1 – Pointer to first buffer to compare.

  • buf2 – Pointer to second buffer to compare.

  • len – Number of bytes to compare.


true if the buffers are equal, false if not equal.

cryptx_bytes_compare(buf1, buf2, bytes_to_compare);

bool cryptx_bytes_tostring(const void *buf, size_t len, char *hexstr)

Convert a bytearray to its hexstring representation.

  • buf – Pointer to bytearray to convert.

  • len – Byte length of digest.

  • hexstr – Buffer to write the output hex string to.

// assume `arr` is a bytearray

// allocate buffer for string twice length of bytearray
// plus an addition byte for null termination
char hexstr[sizeof(arr) * 2 + 1];

cryptx_bytes_tostring(arr, sizeof(arr), hexstr);

bool cryptx_bytes_rcopy(void *dest, const void *src, size_t len)

Copies len bytes from src to dest while reversing the byte order.

  • dest – Pointer to a buffer to write bytes.

  • src – Pointer to a buffer to read bytes from.

  • len – Number of bytes to read.

bool cryptx_bytes_reverse(void *buf, size_t len)

Reverses the byte order of a buffer in-place.

  • buf – Pointer to buffer to reverse.

  • len – Length of the buffer.

// assume `arr1` is a bytearray
// assume `arr2` is another reserved buffer of same length

// makes a copy of arr2 with byteorder reversed
cryptx_bytes_rcopy(arr2, arr1, sizeof(arr2));

// reverses arr1 in-place
cryptx_bytes_reverse(arr1, sizeof(arr1));