randomBytesAsHexString function

String randomBytesAsHexString(
  1. int length, {
  2. Random? random,
})

Generates a hex string of length random bytes.

By default, cryptographically secure SecureRandom.fast is used.

Implementation

String randomBytesAsHexString(int length, {Random? random}) {
  final sb = StringBuffer();
  random ??= SecureRandom.safe;
  for (var i = 0; i < length; i++) {
    final x = random.nextInt(256);
    sb.write(_hexChars[x >> 4]);
    sb.write(_hexChars[0xF & x]);
  }
  return sb.toString();
}