1-
// Copyright 2021 TensionDev <[email protected] > 1+ // SPDX-License-Identifier: Apache-2.0
2+ //
3+ // Copyright 2021 TensionDev <[email protected] > 24//
35// Licensed under the Apache License, Version 2.0 (the "License");
46// you may not use this file except in compliance with the License.
@@ -23,10 +25,10 @@ public class UUIDv1
2325 {
2426 protected internal static System . Net . NetworkInformation . PhysicalAddress s_physicalAddress = System . Net . NetworkInformation . PhysicalAddress . None ;
2527 protected internal static Int32 s_clock = Int32 . MinValue ;
26- protected internal static DateTime s_epoch = new DateTime ( 1582 , 10 , 15 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
28+ protected internal static readonly DateTime s_epoch = new DateTime ( 1582 , 10 , 15 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
2729
28- protected internal static Object s_initLock = new Object ( ) ;
29- protected internal static Object s_clockLock = new Object ( ) ;
30+ protected internal static readonly Object s_initLock = new Object ( ) ;
31+ protected internal static readonly Object s_clockLock = new Object ( ) ;
3032
3133 /// <summary>
3234 /// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
@@ -37,68 +39,6 @@ public static Uuid NewUUIDv1()
3739 return NewUUIDv1 ( DateTime . UtcNow ) ;
3840 }
3941
40- /// <summary>
41- /// Initialises the 48-bit Node ID and returns it.<br />
42- /// Returns the MAC Address of a Network Interface Card, if available.
43- /// Otherwise, returns a randomly genrated 48-bit Node ID.
44- /// </summary>
45- /// <returns>A byte-array representing the 48-bit Node ID</returns>
46- public static Byte [ ] GetNodeID ( )
47- {
48- if ( System . Net . NetworkInformation . PhysicalAddress . None . Equals ( s_physicalAddress ) )
49- {
50- System . Net . NetworkInformation . NetworkInterface [ ] networkInterfaces = System . Net . NetworkInformation . NetworkInterface . GetAllNetworkInterfaces ( ) ;
51- if ( networkInterfaces . Length > 0 )
52- {
53- s_physicalAddress = networkInterfaces [ 0 ] . GetPhysicalAddress ( ) ;
54- }
55- else
56- {
57- using ( System . Security . Cryptography . RNGCryptoServiceProvider cryptoServiceProvider = new System . Security . Cryptography . RNGCryptoServiceProvider ( ) )
58- {
59- Byte [ ] fakeNode = new Byte [ 6 ] ;
60- cryptoServiceProvider . GetBytes ( fakeNode ) ;
61- fakeNode [ 0 ] = ( Byte ) ( fakeNode [ 0 ] | 0x01 ) ;
62- s_physicalAddress = new System . Net . NetworkInformation . PhysicalAddress ( fakeNode ) ;
63- }
64- }
65- }
66-
67- return s_physicalAddress . GetAddressBytes ( ) ;
68- }
69-
70- /// <summary>
71- /// Intialises the 14-bit Clock Sequence and returns the current value with the Variant.<br />
72- /// Will return an incremented Clock Sequence on each call, modulo 14-bit.
73- /// </summary>
74- /// <returns>A byte-array representing the 14-bit Clock Sequence, together with the Variant</returns>
75- public static Byte [ ] GetClockSequence ( )
76- {
77- lock ( s_initLock )
78- {
79- if ( s_clock < 0 )
80- {
81- using ( System . Security . Cryptography . RNGCryptoServiceProvider cryptoServiceProvider = new System . Security . Cryptography . RNGCryptoServiceProvider ( ) )
82- {
83- Byte [ ] clockInit = new Byte [ 4 ] ;
84- cryptoServiceProvider . GetBytes ( clockInit ) ;
85- s_clock = BitConverter . ToInt32 ( clockInit , 0 ) & 0x3FFF ;
86- s_clock |= 0x8000 ;
87- }
88- }
89- }
90-
91- Int32 result ;
92- lock ( s_clockLock )
93- {
94- result = s_clock ++ ;
95- if ( s_clock >= 0xC000 )
96- s_clock = 0x8000 ;
97- }
98-
99- return BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( ( Int16 ) result ) ) ;
100- }
101-
10242 /// <summary>
10343 /// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given date and time.
10444 /// </summary>
@@ -144,10 +84,10 @@ public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
14484 if ( nodeID . Length < 6 )
14585 throw new ArgumentException ( String . Format ( "Node ID contains less than 48-bit: {0} bytes" , nodeID . Length ) , nameof ( nodeID ) ) ;
14686
147- TimeSpan timesince = dateTime . ToUniversalTime ( ) - s_epoch . ToUniversalTime ( ) ;
148- Int64 timeinterval = timesince . Ticks ;
87+ TimeSpan timeSince = dateTime . ToUniversalTime ( ) - s_epoch . ToUniversalTime ( ) ;
88+ Int64 timeInterval = timeSince . Ticks ;
14989
150- Byte [ ] time = BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( timeinterval ) ) ;
90+ Byte [ ] time = BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( timeInterval ) ) ;
15191
15292 Byte [ ] hex = new Byte [ 16 ] ;
15393
@@ -176,5 +116,67 @@ public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
176116
177117 return Id ;
178118 }
119+
120+ /// <summary>
121+ /// Initialises the 48-bit Node ID and returns it.<br />
122+ /// Returns the MAC Address of a Network Interface Card, if available.
123+ /// Otherwise, returns a randomly genrated 48-bit Node ID.
124+ /// </summary>
125+ /// <returns>A byte-array representing the 48-bit Node ID</returns>
126+ public static Byte [ ] GetNodeID ( )
127+ {
128+ if ( System . Net . NetworkInformation . PhysicalAddress . None . Equals ( s_physicalAddress ) )
129+ {
130+ System . Net . NetworkInformation . NetworkInterface [ ] networkInterfaces = System . Net . NetworkInformation . NetworkInterface . GetAllNetworkInterfaces ( ) ;
131+ if ( networkInterfaces . Length > 0 )
132+ {
133+ s_physicalAddress = networkInterfaces [ 0 ] . GetPhysicalAddress ( ) ;
134+ }
135+ else
136+ {
137+ using ( System . Security . Cryptography . RNGCryptoServiceProvider cryptoServiceProvider = new System . Security . Cryptography . RNGCryptoServiceProvider ( ) )
138+ {
139+ Byte [ ] fakeNode = new Byte [ 6 ] ;
140+ cryptoServiceProvider . GetBytes ( fakeNode ) ;
141+ fakeNode [ 0 ] = ( Byte ) ( fakeNode [ 0 ] | 0x01 ) ;
142+ s_physicalAddress = new System . Net . NetworkInformation . PhysicalAddress ( fakeNode ) ;
143+ }
144+ }
145+ }
146+
147+ return s_physicalAddress . GetAddressBytes ( ) ;
148+ }
149+
150+ /// <summary>
151+ /// Intialises the 14-bit Clock Sequence and returns the current value with the Variant.<br />
152+ /// Will return an incremented Clock Sequence on each call, modulo 14-bit.
153+ /// </summary>
154+ /// <returns>A byte-array representing the 14-bit Clock Sequence, together with the Variant</returns>
155+ public static Byte [ ] GetClockSequence ( )
156+ {
157+ lock ( s_initLock )
158+ {
159+ if ( s_clock < 0 )
160+ {
161+ using ( System . Security . Cryptography . RNGCryptoServiceProvider cryptoServiceProvider = new System . Security . Cryptography . RNGCryptoServiceProvider ( ) )
162+ {
163+ Byte [ ] clockInit = new Byte [ 4 ] ;
164+ cryptoServiceProvider . GetBytes ( clockInit ) ;
165+ s_clock = BitConverter . ToInt32 ( clockInit , 0 ) & 0x3FFF ;
166+ s_clock |= 0x8000 ;
167+ }
168+ }
169+ }
170+
171+ Int32 result ;
172+ lock ( s_clockLock )
173+ {
174+ result = s_clock ++ ;
175+ if ( s_clock >= 0xC000 )
176+ s_clock = 0x8000 ;
177+ }
178+
179+ return BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( ( Int16 ) result ) ) ;
180+ }
179181 }
180182}
0 commit comments