safeTempName function

String safeTempName({
  1. int length = 12,
})

Returns a short, unpredictable alphanumeric string suitable for temp file names. Backed by Random.secure so names cannot be guessed.

Throws ArgumentError if length is not positive — a zero/negative length cannot be collision-resistant. Audited: 2026-06-12 11:26 EDT

Implementation

String safeTempName({int length = 12}) {
  if (length <= 0) {
    throw ArgumentError.value(length, 'length', 'must be > 0');
  }
  const String chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  return List.generate(length, (_) => chars[_random.nextInt(chars.length)]).join();
}