Skip to content

Random functions

random_u32()

Generate a random 32-bit unsigned integer.

Returns:

  • int

    A random 32-bit unsigned integer between 0 and 0xffffffff (included).

random_uniform(upper_bound)

Generate a random number uniformly distributed in the range [0, upper_bound).

upper_bound must be a positive integer with size uint32_t.

Parameters:

  • upper_bound (int) –

    The upper bound (exclusive) for the random number.

Returns:

  • int

    A random integer in the range [0, upper_bound).

randomize_buffer(buf)

Fill a buffer with random bytes.

Parameters:

  • buf (Buffer) –

    A buffer to fill with random bytes. The buffer must be writable.

gen_random_buffer(size)

Generate a random buffer of the specified size.

Parameters:

  • size (int) –

    The size of the buffer to generate.

Returns:

  • bytes

    A bytes object containing random data of the specified size.

shuffle_buffer(buf)

Shuffle a buffer in place.

Parameters:

  • buf (Buffer) –

    A buffer to shuffle. The buffer must be writable.

pad(buf, blocksize=8192)

Pad a buffer to a multiple of the specified block size.

Parameters:

  • buf (Buffer) –

    A buffer to pad.

  • blocksize (int, default: 8192 ) –

    The block size to pad to (default is 8192).

Returns:

  • bytes

    A bytes object containing the padded buffer.

Raises:

unpad(buf, blocksize=8192)

Unpad a buffer that was padded to a multiple of the specified block size.

Parameters:

  • buf (Buffer) –

    A padded buffer to unpad.

  • blocksize (int, default: 8192 ) –

    The block size that was used for padding (default is 8192).

Returns:

  • bytes

    A bytes object containing the unpadded buffer.

Raises:

  • ValueError

    If the buffer is not padded correctly or if blocksize is invalid.