![]() |
VOOZH | about |
dotnet add package PrettyId --version 2.0.1
NuGet\Install-Package PrettyId -Version 2.0.1
<PackageReference Include="PrettyId" Version="2.0.1" />
<PackageVersion Include="PrettyId" Version="2.0.1" />Directory.Packages.props
<PackageReference Include="PrettyId" />Project file
paket add PrettyId --version 2.0.1
#r "nuget: PrettyId, 2.0.1"
#:package PrettyId@2.0.1
#addin nuget:?package=PrettyId&version=2.0.1Install as a Cake Addin
#tool nuget:?package=PrettyId&version=2.0.1Install as a Cake Tool
<div align="center"> <img src="https://raw.githubusercontent.com/jchristn/PrettyId/refs/heads/main/assets/icon.png?raw=true" width="182" height="182"> </div>
A .NET library for generating human-friendly, customizable random identifiers.
PrettyId creates clean, readable IDs that are easier to work with than raw GUIDs. Perfect for API keys, short URLs, session tokens, database identifiers, and anywhere you need IDs that are both random and pleasant to look at.
dotnet add package PrettyId
using PrettyId;
// Create a generator instance
IdGenerator generator = new IdGenerator();
// Generate a simple 32-character ID
string id = generator.Generate();
// Example: "5wS4rcgWk6Tr0CO0sfXgA0NAtlOp60C"
// Generate with a prefix
string userId = generator.Generate("user_", 32);
// Example: "user_mAg6shuO0GSplEn7GmX"
// Generate longer IDs for more entropy
string apiKey = generator.Generate("api_", 64);
// Example: "api_KL5ULxfC2kujhcMtDKnDKgUAANsBAdESqJBDKIgvLwdxfjo03uJEK"
IdGenerator generator = new IdGenerator();
string apiKey = generator.Generate("sk_", 48);
// sk_T5wR8pqLmN4vXzKj9hYbGcFdW3sA2eU1iO7uP
string accessToken = generator.Generate("tok_", 64);
// tok_xM9nL2kJ7vC5bH1zT4qR8wG6fD3sY0pE5uA7iO2mK4tN6hB1gF
K-sortable IDs include a timestamp prefix, making them sort chronologically in your database:
IdGenerator generator = new IdGenerator();
string orderId = generator.GenerateKSortable("order_", 48);
// order_1lx4f0h_kL9mT3pR7wX2vN5bH8qJ1cF
string invoiceId = generator.GenerateKSortable("inv_", 48);
// inv_1lx4f0k_dG4sW9tY6mL2nB7xC3vF5kP
The IDs sort chronologically because the timestamp comes first. Great for database indexes and pagination!
IdGenerator generator = new IdGenerator();
// Perfect for short URLs
string shortUrl = generator.GenerateUrlSafe("s_", 16);
// s_kLm3Pq9R
// Share codes
string shareCode = generator.GenerateUrlSafe(null, 12);
// Rf9kLm3Pq9kT
IdGenerator generator = new IdGenerator();
generator.UseCryptographicRng = true; // Extra security for session IDs
string sessionId = generator.Generate("sess_", 48);
// sess_xQ9kLm8vT3pR7wX2nB5hJ1cF4gD6sY0uE
Need specific character constraints? Configure your own character set (minimum 16 unique characters):
// Hexadecimal identifiers
IdGenerator hexGenerator = new IdGenerator(DefaultCharacterSets.HexadecimalLowercase);
string hexId = hexGenerator.Generate("key_", 32);
// key_7a3f2d1e9c4b8a6d5e0f1c2a
// URL-safe identifiers
IdGenerator urlSafeGenerator = new IdGenerator(DefaultCharacterSets.UrlSafe);
string token = urlSafeGenerator.Generate("tok_", 32);
// tok_kLm3Pq9R.wX2nB5hJ1cF4gD6sY
Create multiple generator instances with different configurations:
// API keys: long and cryptographically secure
IdGenerator apiKeyGen = new IdGenerator();
apiKeyGen.UseCryptographicRng = true;
// User IDs: shorter, alphanumeric
IdGenerator userIdGen = new IdGenerator(DefaultCharacterSets.EnglishAlphanumeric, maximumLength: 24);
// Order IDs: k-sortable for database performance
IdGenerator orderIdGen = new IdGenerator();
// Use them independently
string apiKey = apiKeyGen.Generate("sk_", 64);
string userId = userIdGen.Generate("user_", 24);
string orderId = orderIdGen.GenerateKSortable("order_", 48);
DefaultCharacterSets.EnglishAlphanumeric // a-z, A-Z, 0-9 (default)
DefaultCharacterSets.Base64 // Alphanumeric + +/=
DefaultCharacterSets.USKeyboard // All typeable US keyboard characters
DefaultCharacterSets.HexadecimalUppercase // 0-9, A-F
DefaultCharacterSets.HexadecimalLowercase // 0-9, a-f
DefaultCharacterSets.UrlSafe // Alphanumeric + -_.~
| Method | Description | Example Output |
|---|---|---|
Generate() |
Standard ID generation | 5wS4rcgWk6Tr0CO0sfXgA0NAtl |
Generate(prefix, length) |
ID with custom prefix | user_mAg6shuO0GSplEn7GmX |
GenerateUrlSafe() |
URL-safe characters only | kLm3Pq-9R_wX.vN |
GenerateKSortable() |
Timestamp + random (database-friendly) | order_1lx4f0h_kL9mT3pR7wX |
GenerateAndEncodeBase64() |
Random string encoded as base64 | b64_VGhpc0lzUmFuZG9t |
IdGenerator generator = new IdGenerator();
// Set valid characters
generator.ValidCharacters = new HashSet<char>(DefaultCharacterSets.HexadecimalLowercase);
// Set default length
generator.MaximumLength = 48;
// Use cryptographically secure randomness
generator.UseCryptographicRng = true;
// MaxIterations retained for backward compatibility (no longer used internally)
generator.MaxIterations = 128;
PrettyId does NOT guarantee uniqueness by checking against previously generated IDs. It relies on the statistical improbability of collision, similar to GUIDs.
PrettyId is designed to be fast:
StringBuilder for efficient string buildingRandomNumberGenerator instance (no per-call allocation)Performance Tips:
✅ All Generate* methods are thread-safe — they do not modify instance state
⚠️ Not thread-safe for writing — don't modify properties (ValidCharacters, MaximumLength, etc.) from multiple threads
// Safe: Multiple threads reading
IdGenerator generator = new IdGenerator();
Parallel.For(0, 1000, i => {
string id = generator.Generate(); // ✅ Thread-safe
});
// Unsafe: Modifying from multiple threads
Parallel.For(0, 10, i => {
generator.MaximumLength = 32 + i; // ⚠️ Not thread-safe
});
// Safe: Each thread has its own instance
Parallel.For(0, 1000, i => {
IdGenerator generator = new IdGenerator(); // ✅ Thread-safe
string id = generator.Generate();
});
# Build the library
dotnet build src/PrettyId/PrettyId.csproj
# Run the test/demo program
dotnet run --project src/Test/Test.csproj
# Build release (generates NuGet package)
dotnet build src/PrettyId/PrettyId.csproj -c Release
PrettyId supports a wide range of .NET frameworks:
Contributions are welcome! Please file issues and pull requests at: https://github.com/jchristn/PrettyId
Ideas for new default character sets? Found a bug? Want to suggest a feature? Open an issue!
MIT License - see LICENSE.md for details
Need help? File an issue at https://github.com/jchristn/PrettyId/issues
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 is compatible. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 is compatible. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on PrettyId:
| Package | Downloads |
|---|---|
|
S3Server
Emulated Amazon Web Services (AWS) Simple Storage Service (S3) server-side interface. |
|
|
LiteGraph
LiteGraph is a property graph database with support for graph relationships, tags, labels, metadata, data, and vectors. |
|
|
View.Sdk
C# SDK for View AI. |
|
|
LiteGraph.Sdk
SDK for LiteGraph, a property graph database with support for graph relationships, tags, labels, metadata, data, and vectors. |
|
|
S3Lite
Lightweight Amazon S3 client without the heft and dependency drag of the official library. |
Showing the top 1 popular GitHub repositories that depend on PrettyId:
| Repository | Stars |
|---|---|
|
litegraphdb/litegraph
Lightweight graph database with relational, vector, and MCP support, designed to power knowledge and artificial intelligence persistence and retrieval.
|
Add generation of valid base64 values.