https://github.com/kpol/passwordgenerator
A lightweight, secure, and flexible password generator library for .NET, built with cryptographic randomness and fully customizable character sets.
https://github.com/kpol/passwordgenerator
csharp password password-generator security
Last synced: 11 months ago
JSON representation
A lightweight, secure, and flexible password generator library for .NET, built with cryptographic randomness and fully customizable character sets.
- Host: GitHub
- URL: https://github.com/kpol/passwordgenerator
- Owner: kpol
- License: mit
- Created: 2025-06-28T02:38:07.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-07T02:20:00.000Z (11 months ago)
- Last Synced: 2025-07-07T03:32:34.211Z (11 months ago)
- Topics: csharp, password, password-generator, security
- Language: C#
- Homepage:
- Size: 27.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PasswordGenerator
A lightweight, secure, and flexible password generator library for .NET, built with cryptographic randomness and fully customizable character sets.
[](https://github.com/kpol/PasswordGenerator/actions)
[](https://www.nuget.org/packages/KPasswordGenerator)
---
## Features
- **Cryptographically secure** — built on .NET’s [`RandomNumberGenerator`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator) for high-entropy randomness
- **Performance-focused** — uses `Span` and stack allocation to minimize heap usage and avoid unnecessary string allocations
- **Customizable character sets** — define your own character pools (e.g., symbols, digits, uppercase)
- **Per-set minimum requirements** — ensure that a minimum number of characters from each set are included
- **Predictable-free output** — final password is securely shuffled to eliminate character order patterns
- **Ambiguity-avoiding** — easily exclude confusing characters like `0`, `O`, `l`, and `I`
- Clean, testable design — .NET 8 compatible and unit-tested
---
```csharp
using KPasswordGenerator;
// Define your password policy
PasswordSettings settings = new(
[
new(minRequired: 2, characterPool: "ABCDEFGHJKLMNPQRSTUVWXYZ"),
new(3, "abcdefghijkmnopqrstuvwxyz"), // At least 3 lowercase letters (no l)
new(4, "23456789"), // At least 4 digits (no 0, 1)
new(2, "!@$?_-") // At least 2 symbols
]);
PasswordGenerator generator = new(settings);
string password = generator.Generate(passwordLength: 16);
Console.WriteLine(password); // Example output: kAj79uV@E?m7_8eS
```
### Using WithDefaults
Quickly generate a password using default pools:
```csharp
PasswordSettings settings = PasswordSettings.WithDefaults(minLower: 2, minUpper: 2, minDigits: 2, minSpecial: 1);
PasswordGenerator generator = new(settings);
string password = generator.Generate(12);
```
## Additional Methods
### `GenerateRandomLength(int minPasswordLength, int maxPasswordLength)`
Generates a password of **random length within a specified range**.
- Ensures the generated password meets all defined character requirements.
- Uses secure randomness to choose the length.
**Example:**
```csharp
string password = generator.GenerateRandomLength(12, 20);
```
---
### `Validate(string password)`
Validates whether a password meets the current `PasswordSettings` policy.
- Checks overall length.
- Verifies that the required number of characters from each character pool are present.
- Returns `true` if all rules pass, otherwise `false`.
- **Example:**
```csharp
bool isValid = generator.Validate("abcD123!");
```
## Installation
Install via NuGet:
```bash
dotnet add package KPasswordGenerator