|
1 | | -# TensionDev.UUID |
| 1 | +# TensionDev.UUID |
2 | 2 |
|
3 | 3 | [](https://github.com/TensionDev/UUIDUtil/actions/workflows/dotnet.yml) |
4 | 4 | [](https://github.com/TensionDev/UUIDUtil/actions/workflows/package-release.yml) |
5 | 5 | [](https://github.com/TensionDev/UUIDUtil/actions/workflows/github-code-scanning/codeql) |
6 | 6 |
|
7 | | -A project to store UUID functions within a library for future use. |
| 7 | +TensionDev.UUID is a .NET library for working with Universally Unique Identifiers (UUIDs). |
8 | 8 | This project references the following documents for implementation. |
9 | 9 | - [Universally unique identifier - Wikipedia](https://en.wikipedia.org/wiki/Universally_unique_identifier) |
10 | 10 | - [MySQL :: MySQL 8.0 Reference Manual :: 12.24 Miscellaneous Functions](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid) |
11 | 11 | - [rfc4122](https://datatracker.ietf.org/doc/html/rfc4122) |
12 | 12 | - [rfc9562](https://datatracker.ietf.org/doc/html/rfc9562) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Support for multiple UUID versions: |
| 19 | + - v1 (date-time and MAC address) |
| 20 | + - v3 (MD5 namespace name-based) |
| 21 | + - v4 (random) |
| 22 | + - v5 (SHA-1 namespace name-based) |
| 23 | + - v6 (date-time-ordered) |
| 24 | + - v7 (Unix time + randomness) |
| 25 | +- Converters: |
| 26 | + - `ToGuid()` and `FromGuid(Guid)` |
| 27 | + - `ToByteArray()` and `FromBytes(byte[])` |
| 28 | + - Variant conversions (`ToVariant1`, `ToVariant2`) |
| 29 | +- Parsing utilities: |
| 30 | + - `Parse(string)` |
| 31 | + - `TryParse(string, out Uuid)` |
| 32 | +- Formatting options: |
| 33 | + - `N`, `D`, `B`, `P` (same as System.Guid) |
| 34 | +- Comparison operators: |
| 35 | + - `==`, `!=`, `<`, `>`, `<=`, `>=` |
| 36 | +- Equality and hashing for dictionary/set usage |
| 37 | +- Strict RFC compliance for parsing and validation |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Installation |
| 42 | +``` |
| 43 | +dotnet add package TensionDev.UUID |
| 44 | +``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Usage Examples |
| 49 | + |
| 50 | +### Generate UUID v1 (date-time and MAC address) |
| 51 | +```csharp |
| 52 | +using TensionDev.UUID; |
| 53 | + |
| 54 | +Uuid uuid = UUIDv1.NewUUIDv1(); |
| 55 | +Console.WriteLine(uuid); // Example: 164a714c-0c79-11ec-82a8-0242ac130003 |
| 56 | +``` |
| 57 | + |
| 58 | +### Generate UUID v4 (random) |
| 59 | +```csharp |
| 60 | +using TensionDev.UUID; |
| 61 | + |
| 62 | +Uuid uuid = UUIDv4.NewUUIDv4(); |
| 63 | +Console.WriteLine(uuid); // Example: 550e8400-e29b-41d4-a716-446655440000 |
| 64 | +``` |
| 65 | + |
| 66 | +### Generate UUID v5 (SHA-1 namespace name-based) |
| 67 | +```csharp |
| 68 | +using TensionDev.UUID; |
| 69 | + |
| 70 | +Uuid uuid = UUIDv5.NewUUIDv5(UUIDNamespace.URL, "https://www.contoso.com"); |
| 71 | +Console.WriteLine(uuid); // Example: 1bf6935b-49e6-54cf-a9c8-51fb21c41b46 |
| 72 | +``` |
| 73 | + |
| 74 | +### Parse and validate |
| 75 | +```csharp |
| 76 | +using TensionDev.UUID; |
| 77 | + |
| 78 | +bool isValid = Uuid.TryParse("550e8400-e29b-41d4-a716-446655440000", out var parsed); |
| 79 | +``` |
| 80 | + |
| 81 | +### Byte Conversions |
| 82 | +```csharp |
| 83 | +using TensionDev.UUID; |
| 84 | + |
| 85 | +byte[] bytes = uuid.ToByteArray(); |
| 86 | +Uuid fromBytes = Uuid.FromBytes(bytes); |
| 87 | +``` |
| 88 | + |
| 89 | +### String Formatting |
| 90 | +```csharp |
| 91 | +using TensionDev.UUID; |
| 92 | + |
| 93 | +Console.WriteLine(uuid.ToString("N")); // 32 hex digits |
| 94 | +Console.WriteLine(uuid.ToString("D")); // canonical form |
| 95 | +Console.WriteLine(uuid.ToString("B")); // with braces |
| 96 | +Console.WriteLine(uuid.ToString("P")); // with parentheses |
| 97 | +``` |
| 98 | + |
0 commit comments