randomBytes method

Uint8List randomBytes(
  1. int length
)

Random bytes from OpenSSL's RNG.

Implementation

Uint8List randomBytes(int length) {
  if (length <= 0) {
    throw ArgumentError.value(length, 'length', 'must be positive');
  }
  final buffer = calloc<UnsignedChar>(length);
  try {
    if (bindings.RAND_bytes(buffer, length) != 1) {
      throw OpenSslException('RAND_bytes failed');
    }
    return Uint8List.fromList(buffer.cast<Uint8>().asTypedList(length));
  } finally {
    calloc.free(buffer);
  }
}