fillBytesWithSecureRandom function

void fillBytesWithSecureRandom(
  1. Uint8List bytes, {
  2. Random? random,
})

Fills a list with random bytes (using Random.secure().

Implementation

void fillBytesWithSecureRandom(Uint8List bytes, {Random? random}) {
  random ??= SecureRandom.safe;
  for (var i = 0; i < bytes.length;) {
    if (i + 3 < bytes.length) {
      // Read 32 bits at a time.
      final x = random.nextInt(_bit32);
      bytes[i] = x >> 24;
      bytes[i + 1] = 0xFF & (x >> 16);
      bytes[i + 2] = 0xFF & (x >> 8);
      bytes[i + 3] = 0xFF & x;
      i += 4;
    } else {
      // Read 8 bits at a time.
      bytes[i] = random.nextInt(0x100);
      i++;
    }
  }
}