randomBytesBuf method

Uint8List randomBytesBuf(
  1. int size
)

Implementation

Uint8List randomBytesBuf(int size) {
  // Allocate memory for the binary data
  final buffer = calloc<ffi.Uint8>();

  try {
    // Call sodium_hex2bin to perform the conversion
    randombytes_buf(buffer.cast<ffi.Void>(), size);
    final binData = buffer.asTypedList(size);

    // Clone the original list
    return Uint8List.fromList(List.from(binData));
  } finally {
    // Free allocated memory
    calloc.free(buffer);
  }
}