11using System . Buffers . Binary ;
22using System . Runtime . CompilerServices ;
33using System . Runtime . Intrinsics ;
4- using System . Runtime . InteropServices ;
5- using System ;
64
75namespace DaanV2 . UUID ;
86
@@ -20,19 +18,19 @@ public static UUID Generate() {
2018 /// Generates a Version 2 UUID with custom timestamp, UID, and MAC address.
2119 /// </summary>
2220 [ MethodImpl ( MethodImplOptions . AggressiveOptimization ) ]
23- public static UUID Generate ( DateTime timestamp , uint localIdentifier , ReadOnlySpan < byte > macAddress , byte domain = 0 ) {
24- ulong uuidTimestamp = V1 . TimeStamp ( timestamp ) ;
21+ public static UUID Generate ( DateTime timestamp , UInt32 localIdentifier , ReadOnlySpan < Byte > macAddress , Byte domain = 0 ) {
22+ UInt64 uuidTimestamp = V1 . TimeStamp ( timestamp ) ;
2523
26- Span < byte > data = stackalloc byte [ 16 ] ;
24+ Span < Byte > data = stackalloc Byte [ 16 ] ;
2725
2826 // Write node (MAC address)
2927 macAddress . CopyTo ( data [ 10 ..] ) ;
3028
3129 // Split timestamp
32- uint timeLow = ( uint ) ( uuidTimestamp & 0xFFFFFFFF ) ;
33- ushort timeMid = ( ushort ) ( ( uuidTimestamp >> 32 ) & 0xFFFF ) ;
34- ushort timeHi = ( ushort ) ( ( uuidTimestamp >> 48 ) & 0x0FFF ) ; // 12 bits for time_hi
35- ushort timeHiAndVersion = ( ushort ) ( timeHi | ( ( ushort ) Version . V2 ) ) ; // Set version 2
30+ UInt32 timeLow = ( UInt32 ) ( uuidTimestamp & 0xFFFFFFFF ) ;
31+ UInt16 timeMid = ( UInt16 ) ( ( uuidTimestamp >> 32 ) & 0xFFFF ) ;
32+ UInt16 timeHi = ( UInt16 ) ( ( uuidTimestamp >> 48 ) & 0x0FFF ) ; // 12 bits for time_hi
33+ UInt16 timeHiAndVersion = ( UInt16 ) ( timeHi | ( ( UInt16 ) Version . V2 ) ) ; // Set version 2
3634
3735 BinaryPrimitives . WriteUInt32BigEndian ( data , timeLow ) ;
3836 BinaryPrimitives . WriteUInt16BigEndian ( data [ 4 ..] , timeMid ) ;
@@ -41,7 +39,7 @@ public static UUID Generate(DateTime timestamp, uint localIdentifier, ReadOnlySp
4139 // Per RFC 4122 DCE Security UUID:
4240 // data[8]: high 2 bits = variant, low 6 bits = high 6 bits of local identifier
4341 // data[9]: domain (per spec), or low 8 bits of local identifier if domain not used
44- data [ 8 ] = ( byte ) ( ( ( localIdentifier >> 8 ) & 0x3F ) | ( byte ) Variant . V1 ) ;
42+ data [ 8 ] = ( Byte ) ( ( ( localIdentifier >> 8 ) & 0x3F ) | ( Byte ) Variant . V1 ) ;
4543 data [ 9 ] = domain ; // Store domain in data[9]
4644
4745 var uuid = Vector128 . Create < Byte > ( data ) ;
@@ -51,10 +49,11 @@ public static UUID Generate(DateTime timestamp, uint localIdentifier, ReadOnlySp
5149 /// <summary>
5250 /// Gets the current user's UID (Linux only). Returns 0 if not available.
5351 /// </summary>
54- private static uint GetCurrentUid ( ) {
52+ private static UInt32 GetCurrentUid ( ) {
5553 try {
56- return ( uint ) System . Convert . ToInt32 ( Environment . GetEnvironmentVariable ( "UID" ) ?? "0" ) ;
57- } catch {
54+ return ( UInt32 ) System . Convert . ToInt32 ( Environment . GetEnvironmentVariable ( "UID" ) ?? "0" ) ;
55+ }
56+ catch {
5857 return 0 ;
5958 }
6059 }
@@ -65,11 +64,11 @@ private static uint GetCurrentUid() {
6564 [ MethodImpl ( MethodImplOptions . AggressiveOptimization | MethodImplOptions . AggressiveInlining ) ]
6665 public static UUID Generate ( ReadOnlySpan < Byte > source ) {
6766 // Not standard for V2, but for API compatibility: hash and overlay version/variant
68- Span < byte > data = stackalloc byte [ 16 ] ;
67+ Span < Byte > data = stackalloc Byte [ 16 ] ;
6968 System . Security . Cryptography . MD5 . HashData ( source , data ) ;
7069 // Set version and variant using V2.Version and V2.Variant constants
71- data [ 6 ] = ( byte ) ( ( data [ 6 ] & 0x0F ) | ( byte ) Version . V2 ) ;
72- data [ 8 ] = ( byte ) ( ( data [ 8 ] & 0x3F ) | ( byte ) Variant . V1 ) ;
70+ data [ 6 ] = ( Byte ) ( ( data [ 6 ] & 0x0F ) | ( Byte ) Version . V2 ) ;
71+ data [ 8 ] = ( Byte ) ( ( data [ 8 ] & 0x3F ) | ( Byte ) Variant . V1 ) ;
7372
7473 var uuid = Vector128 . Create < Byte > ( data ) ;
7574 return new UUID ( uuid ) ;
0 commit comments