Skip to main content

Overview

LocalRegex provides a secure password generation utility that creates random passwords containing uppercase letters, lowercase letters, numbers, and special characters.

Method signature

static String generatePassword({int length = 12})

Parameters

length
int
default:"12"
The desired length of the generated password. Must be a positive integer.

Character sets

The generated password includes characters from all of the following sets:
  • Lowercase letters: a-z
  • Uppercase letters: A-Z
  • Numbers: 0-9
  • Special characters: !@#$%^&*()-_=+[]{}|;:",.<>?/~ `
The password generator uses Random.secure() to ensure cryptographically secure random number generation.

Basic usage

Generate default password

Generate a 12-character password (default length):
import 'package:localregex/localregex.dart';

void main() {
  final password = LocalRegex.generatePassword();
  print(password);
  // Example output: "aB3$xY9!mK2@"
}

Generate custom length password

Generate a password with a specific length:
import 'package:localregex/localregex.dart';

void main() {
  // Generate a 16-character password
  final password = LocalRegex.generatePassword(length: 16);
  print(password);
  // Example output: "xP9$mK2@aB3!yC7#"
  
  // Generate a short 8-character password
  final shortPassword = LocalRegex.generatePassword(length: 8);
  print(shortPassword);
  // Example output: "aB3$xY9!"
}

Use cases

User registration

Generate temporary passwords for new user accounts

Password reset

Create secure temporary passwords when users request password resets

API keys

Generate random API keys or tokens for authentication

Test data

Create test passwords for development and testing environments

Password strength

The generated passwords are designed to be strong and secure:
Each password is guaranteed to contain a mix of uppercase, lowercase, numbers, and special characters, making them resistant to dictionary attacks.
The Random.secure() generator provides cryptographically strong random numbers suitable for security-sensitive applications.
Longer passwords provide exponentially more possible combinations, increasing security. The default 12 characters provides good security for most use cases.

Example: User registration flow

import 'package:localregex/localregex.dart';

class UserService {
  Future<User> registerUser(String email) async {
    // Generate a secure temporary password
    final tempPassword = LocalRegex.generatePassword(length: 16);
    
    // Create user account
    final user = await createAccount(
      email: email,
      password: tempPassword,
      requirePasswordChange: true,
    );
    
    // Send temporary password to user
    await sendWelcomeEmail(
      email: email,
      tempPassword: tempPassword,
    );
    
    return user;
  }
}
Always encourage users to change automatically generated passwords to their own memorable passwords after first login.

Validation

To validate if a password meets strength requirements, use the isPassword() method:
import 'package:localregex/localregex.dart';

void main() {
  final password = LocalRegex.generatePassword();
  
  // Verify the generated password is strong
  final isStrong = LocalRegex.isPassword(password);
  print('Password is strong: $isStrong'); // true
}
See the Password validation page for more details on password validation.

Build docs developers (and LLMs) love