password static method

String password(
  1. int length, {
  2. bool letters = true,
  3. bool numbers = true,
  4. bool symbols = true,
  5. bool spaces = false,
})

Generates a cryptographically random password.

Toggle the character classes via letters, numbers, symbols, and spaces. At least one class must be enabled.

Implementation

static String password(
  int length, {
  bool letters = true,
  bool numbers = true,
  bool symbols = true,
  bool spaces = false,
}) {
  final pool = StringBuffer();
  if (letters) {
    pool.write('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  }
  if (numbers) pool.write('0123456789');
  if (symbols) pool.write(_passwordSymbols);
  if (spaces) pool.write(' ');
  final chars = pool.toString();
  if (chars.isEmpty || length <= 0) return '';
  final out = StringBuffer();
  for (var i = 0; i < length; i++) {
    out.write(chars[_random.nextInt(chars.length)]);
  }
  return out.toString();
}