secureRandomToken function

String secureRandomToken({
  1. int length = 32,
})

Generates a cryptographically secure random token.

length is the number of random bytes and defaults to 32. The bytes come from Random.secure and are returned as padded URL-safe base64. A negative length causes List.generate to throw RangeError.

Implementation

String secureRandomToken({int length = 32}) {
  final rand = Random.secure();
  final bytes = List<int>.generate(length, (_) => rand.nextInt(256));
  return base64UrlEncode(bytes);
}