id_generator 1.0.2
id_generator: ^1.0.2 copied to clipboard
A comprehensive Dart library for generating secure, customizable random IDs with support for numeric, alphabetic, special characters, and mixed combinations.
ID Generator #
A comprehensive Dart library for generating secure, customizable random IDs with support for numeric, alphabetic, special characters, and mixed combinations.
β¨ Features #
- π’ Numeric IDs: Generate IDs using digits 1-9 (excludes 0 for clarity)
- π€ Alphabetic IDs: Generate IDs with mixed case letters (a-z, A-Z)
- π£ Special Character IDs: Generate IDs using safe special characters
- π Mixed IDs: Combine all character types for maximum entropy
- π― Custom Mix: Create IDs with user-defined character type combinations
- π¦ Batch Generation: Efficiently generate multiple IDs at once
- π Secure: Uses Dart's cryptographically secure Random class
- β‘ Fast: Optimized for performance with minimal overhead
π Getting Started #
Installation #
Add this package to your pubspec.yaml
:
dependencies:
id_generator: latest # Use latest version
π‘ Always latest: Using
latest
ensures you get the most current version. You can also use^1.0.0
for semantic versioning or check pub.flutter-io.cn for specific version numbers.
Then run:
dart pub get
Import #
import 'package:id_generator/id_generator.dart';
π Usage #
Basic Examples #
Numeric IDs
// Generate a 8-character numeric ID
String numericId = IDGenerator.generateNum(8);
print(numericId); // Example: "47382951"
// Generate multiple numeric IDs
List<String> numericIds = IDGenerator.listOfNums(3, 6);
print(numericIds); // Example: ["473829", "194756", "829374"]
Alphabetic IDs
// Generate a 10-character alphabetic ID
String alphaId = IDGenerator.generateAlpha(10);
print(alphaId); // Example: "aBcDeFgHiJ"
// Generate multiple alphabetic IDs
List<String> alphaIds = IDGenerator.listOfAlpha(2, 8);
print(alphaIds); // Example: ["aBcDeFgH", "XyZaBcDe"]
Special Character IDs
// Generate a 6-character special character ID
String specialId = IDGenerator.generateSpecial(6);
print(specialId); // Example: "!@#$%^"
// Generate multiple special character IDs
List<String> specialIds = IDGenerator.listOfSpecial(2, 4);
print(specialIds); // Example: ["!@#$", "^&*_"]
Advanced Usage #
Mixed IDs (All Character Types)
// Generate a 12-character mixed ID
String mixedId = IDGenerator.mix(12);
print(mixedId); // Example: "a7$bC3@dE9!f"
// Generate multiple mixed IDs
List<String> mixedIds = IDGenerator.listOfMix(5, 8);
print(mixedIds); // Example: ["a7$bC3@d", "E9!fG2#h", "K4%mN8&p", ...]
Custom Character Type Combinations
// Generate ID with only numbers and letters (no special chars)
String customId = IDGenerator.customMix(10, [IdgEnum.nums, IdgEnum.alpha]);
print(customId); // Example: "a7bC3dE9fG"
// Generate IDs with only special characters and numbers
List<String> customIds = IDGenerator.listOfCustomMix(
3, 6, [IdgEnum.special, IdgEnum.nums]
);
print(customIds); // Example: ["!@1#2$", "3%4^5&", "*6_7-8"]
// Generate ID with only alphabetic characters
String alphaOnlyId = IDGenerator.customMix(8, [IdgEnum.alpha]);
print(alphaOnlyId); // Example: "aBcDeFgH"
π― Use Cases #
- Session IDs: Secure session identifiers for web applications
- API Keys: Generate random API keys and tokens
- File Names: Create unique file names to avoid conflicts
- Database IDs: Alternative to auto-incrementing primary keys
- Temporary Passwords: Generate temporary passwords for user accounts
- Unique Identifiers: Any scenario requiring unique string identifiers
- Testing: Generate test data with random identifiers
π Character Sets #
Type | Characters | Count | Example |
---|---|---|---|
Numeric | 1-9 |
9 | 473829 |
Alphabetic | a-z, A-Z |
52 | aBcDeFgH |
Special | - _ ! @ # $ % ^ & * |
10 | !@#$%^ |
Mixed | All above combined | 71 | a7$bC3@d |
Note: The digit
0
is intentionally excluded from numeric IDs to avoid confusion with the letterO
in certain contexts.
β‘ Performance #
The ID Generator is optimized for performance:
- Single ID Generation: ~0.1ms for typical lengths (8-16 characters)
- Batch Generation: ~50% faster than individual calls for multiple IDs
- Memory Efficient: Minimal memory allocation during generation
- Thread Safe: All methods are thread-safe and can be used concurrently
π Security Considerations #
- Uses Dart's
Random
class which provides cryptographically secure randomness - Generated IDs have high entropy making them suitable for security-sensitive applications
- Character sets are carefully chosen to avoid common confusion issues
- No predictable patterns in generated sequences
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Development Setup #
- Clone the repository
- Run
dart pub get
to install dependencies - Run
dart test
to execute tests - Run
dart analyze
to check for issues
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π Links #
π Changelog #
See CHANGELOG.md for a detailed list of changes and version history.
Made with β€οΈ for the Dart community