SH.Framework.Library.Generators
1.1.0
dotnet add package SH.Framework.Library.Generators --version 1.1.0
NuGet\Install-Package SH.Framework.Library.Generators -Version 1.1.0
<PackageReference Include="SH.Framework.Library.Generators" Version="1.1.0" />
<PackageVersion Include="SH.Framework.Library.Generators" Version="1.1.0" />
<PackageReference Include="SH.Framework.Library.Generators" />
paket add SH.Framework.Library.Generators --version 1.1.0
#r "nuget: SH.Framework.Library.Generators, 1.1.0"
#:package SH.Framework.Library.Generators@1.1.0
#addin nuget:?package=SH.Framework.Library.Generators&version=1.1.0
#tool nuget:?package=SH.Framework.Library.Generators&version=1.1.0
SH Framework - Generators Library
A secure character and code generator library for .NET applications. Provides cryptographically secure random string generation with support for alphabetic, numeric, alphanumeric, and symbol character sets with customizable formats and patterns.
Features
- Cryptographically Secure: Uses
RandomNumberGenerator
for secure random generation - Multiple Character Types: Support for alphabetic, numeric, alphanumeric, and symbol characters
- Custom Character Sets: Combine multiple character types or provide custom character sets
- Formatted Code Generation: Generate codes with specific patterns using format strings
- High Performance: Optimized for speed and memory efficiency
- Thread Safe: Safe for use in multi-threaded applications
Installation
Install the package via NuGet Package Manager:
dotnet add package SH.Framework.Library.Generators
Or via Package Manager Console:
Install-Package SH.Framework.Library.Generators
Quick Start
using SH.Framework.Library.Generators;
var generator = new CharacterGenerator();
// Generate a 6-character alphanumeric code (default)
var code = generator.Generate();
// Output: "A7xK9m"
// Generate a 10-character numeric code
var numericCode = generator.GenerateNumericCode(10);
// Output: "4729581063"
// Generate with specific character type
var alphabetic = generator.Generate(8, CharacterType.Alphabet);
// Output: "KmPqRsXz"
Usage Examples
Basic Generation
var generator = new CharacterGenerator();
// Default: 6 characters, alphanumeric
var defaultCode = generator.Generate();
// Specify length and character type
var customCode = generator.Generate(12, CharacterType.Alphanumeric);
// Numeric codes (useful for verification codes)
var verificationCode = generator.GenerateNumericCode(6);
Multiple Character Types
// Combine multiple character types
var mixedCode = generator.Generate(10,
CharacterType.Alphabet,
CharacterType.Numeric,
CharacterType.Symbol);
// This will include letters, numbers, and symbols
Custom Character Sets
// Use custom character sets
var customCode = generator.Generate(8, "ABCDEF", "123456");
// Will only use characters A-F and numbers 1-6
// Vowels only
var vowelCode = generator.Generate(6, "AEIOU");
Formatted Code Generation
Generate codes with specific patterns using format strings:
// Format characters:
// A = Any alphabet character (upper or lower case)
// N = Any numeric character
// B = Any alphanumeric character
// L = Any alphabet character (lowercase only)
// S = Any symbol character
// Any other character = literal character
var formattedCode = generator.GenerateFormattedCode("AAA-NNN");
// Output: "KmP-847"
var licenseKey = generator.GenerateFormattedCode("AAAA-BBBB-NNNN");
// Output: "XYZW-A7B9-1234"
var strongPassword = generator.GenerateFormattedCode("LLLLSSNNN");
// Output: "abcd!@123"
Advanced Examples
// Generate a secure API key
var apiKey = generator.Generate(32, CharacterType.Alphanumeric);
// Generate a temporary password with symbols
var tempPassword = generator.Generate(12,
CharacterType.Alphabet,
CharacterType.Numeric,
CharacterType.Symbol);
// Generate a user-friendly code (no ambiguous characters)
var friendlyCode = generator.Generate(8, "ABCDEFGHJKLMNPQRSTUVWXYZ23456789");
// Generate a formatted product key
var productKey = generator.GenerateFormattedCode("AAAA-BBBB-CCCC-DDDD");
Character Types
Type | Characters | Use Case |
---|---|---|
Alphabet |
A-Z, a-z | Letters only |
Numeric |
0-9 | Numbers only (verification codes) |
Alphanumeric |
A-Z, a-z, 0-9 | General purpose codes |
Symbol |
!@#$%^&*()_+-=[]{}|;:,.<>? | Special characters |
Security Features
- Cryptographically Secure: Uses
System.Security.Cryptography.RandomNumberGenerator
for all random data generation. - Uniform Distribution: Ensures a perfectly uniform character distribution by using bias-free random number generation, eliminating vulnerabilities like "modulo bias".
- No Predictable Patterns: The generated output is computationally indistinguishable from a true random sequence.
- Thread-Safe: Safe for use in concurrent, multi-threaded applications.
Performance
The library is optimized for high-performance scenarios and built to be lightweight.
- Optimized Memory Usage: Refactored to minimize memory allocations, especially for generating long strings, resulting in a low memory footprint.
- Efficient Character Set Handling: Processes custom and combined character sets effectively.
- Fast Generation: Leverages modern .NET APIs for high-speed random string creation.
- High-Throughput Ready: Suitable for demanding applications requiring a large volume of generated codes or strings.ns
Requirements
- .NET 9.0 or higher
- No external dependencies (uses only .NET BCL)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
SH Framework - Building robust .NET applications, one library at a time.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
## Version 1.1.0 - Security and Performance Update
### Improvements & Fixes
- **Cryptographic Security Enhancement:** Replaced the internal random number generation logic with `RandomNumberGenerator.GetInt32` to eliminate potential "modulo bias." This ensures a perfectly uniform character distribution, enhancing security for generating tokens and passwords.
- **Performance and Memory Optimization:** The core generation method has been refactored to avoid large memory allocations, significantly improving performance and reducing memory footprint when creating very long strings.
- **Code Cleanup:** Removed unused internal methods, resulting in a cleaner and more maintainable codebase.
## Version 1.0.1 - Initial Release
### Features
- [cite_start]Cryptographically secure character and code generation [cite: 3]
- Support for Alphabet, Numeric, Alphanumeric, and Symbol character types
- Custom character set combinations with multiple overloads
- Formatted code generation with pattern support (A=Alpha, N=Numeric, B=Both, L=Lowercase, S=Symbol)
- High-performance implementation optimized for .NET 9.0
- [cite_start]Thread-safe for multi-threaded applications [cite: 4]
### Security
- Uses System.Security.Cryptography.RandomNumberGenerator
- No predictable patterns in generated output
- Secure for password and token generation
### Methods
- [cite_start]Generate() - Basic generation with length and character type options [cite: 5]
- GenerateNumericCode() - Optimized for verification codes
- GenerateFormattedCode() - Pattern-based generation for structured codes
### Information
- Github Repository URL updated