generate static method

String generate({
  1. int length = 16,
  2. bool includeSymbols = true,
})

Generate a random secure password

length - Length of the generated password (default: 16) includeSymbols - Whether to include special characters (default: true)

Returns a cryptographically secure random password

Implementation

static String generate({int length = 16, bool includeSymbols = true}) {
  if (length < 8) {
    throw ArgumentError('Password length must be at least 8 characters');
  }

  final chars = includeSymbols
      ? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()-_=+[]{}|;:,.<>?'
      : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

  final random = Random.secure();
  return String.fromCharCodes(
    Iterable<int>.generate(
      length,
      (index) => chars.codeUnitAt(random.nextInt(chars.length)),
    ),
  );
}