generatePassword method

String generatePassword({
  1. int length = 16,
  2. bool includeUppercase = true,
  3. bool includeLowercase = true,
  4. bool includeNumbers = true,
  5. bool includeSymbols = true,
})

Generate a secure password

length is the length of the password includeUppercase whether to include uppercase letters includeLowercase whether to include lowercase letters includeNumbers whether to include numbers includeSymbols whether to include symbols

Returns a secure random password

Implementation

String generatePassword({
  int length = 16,
  bool includeUppercase = true,
  bool includeLowercase = true,
  bool includeNumbers = true,
  bool includeSymbols = true,
}) {
  final chars = StringBuffer();

  if (includeUppercase) chars.write('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  if (includeLowercase) chars.write('abcdefghijklmnopqrstuvwxyz');
  if (includeNumbers) chars.write('0123456789');
  if (includeSymbols) chars.write('!@#\$%^&*()_+-=[]{}|;:,.<>?');

  if (chars.isEmpty) {
    throw ArgumentError('At least one character set must be selected');
  }

  final charList = chars.toString().split('');
  final password = StringBuffer();

  for (int i = 0; i < length; i++) {
    final randomIndex = _generateRandomBytes(1)[0] % charList.length;
    password.write(charList[randomIndex]);
  }

  return password.toString();
}