|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace TensionDev.UUID |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 6 (date-time). |
| 7 | + /// </summary> |
| 8 | + public class UUIDv6 |
| 9 | + { |
| 10 | + protected internal static Int32 s_clock = Int32.MinValue; |
| 11 | + protected internal static DateTime s_epoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc); |
| 12 | + |
| 13 | + protected internal static Object s_initLock = new Object(); |
| 14 | + protected internal static Object s_clockLock = new Object(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initialises a new GUID/UUID based on Version 6 (date-time) |
| 18 | + /// </summary> |
| 19 | + /// <returns>A new Uuid object</returns> |
| 20 | + public static Uuid NewUUIDv6() |
| 21 | + { |
| 22 | + return NewUUIDv6(DateTime.UtcNow); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initialises the 48-bit Node ID and returns it.<br /> |
| 27 | + /// Returns a randomly genrated 48-bit Node ID. |
| 28 | + /// </summary> |
| 29 | + /// <returns>A byte-array representing the 48-bit Node ID</returns> |
| 30 | + public static Byte[] GetNodeID() |
| 31 | + { |
| 32 | + using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider()) |
| 33 | + { |
| 34 | + Byte[] fakeNode = new Byte[6]; |
| 35 | + cryptoServiceProvider.GetBytes(fakeNode); |
| 36 | + return fakeNode; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Intialises the 14-bit Clock Sequence and returns the current value with the Variant.<br /> |
| 42 | + /// Will return an incremented Clock Sequence on each call, modulo 14-bit. |
| 43 | + /// </summary> |
| 44 | + /// <returns>A byte-array representing the 14-bit Clock Sequence, together with the Variant</returns> |
| 45 | + public static Byte[] GetClockSequence() |
| 46 | + { |
| 47 | + lock (s_initLock) |
| 48 | + { |
| 49 | + if (s_clock < 0) |
| 50 | + { |
| 51 | + using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider()) |
| 52 | + { |
| 53 | + Byte[] clockInit = new Byte[4]; |
| 54 | + cryptoServiceProvider.GetBytes(clockInit); |
| 55 | + s_clock = BitConverter.ToInt32(clockInit, 0) & 0x3FFF; |
| 56 | + s_clock |= 0x8000; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Int32 result; |
| 62 | + lock (s_clockLock) |
| 63 | + { |
| 64 | + result = s_clock++; |
| 65 | + if (s_clock >= 0xC000) |
| 66 | + s_clock = 0x8000; |
| 67 | + } |
| 68 | + |
| 69 | + return BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((Int16)result)); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Initialises a new GUID/UUID based on Version 6 (date-time), based on the given date and time. |
| 74 | + /// </summary> |
| 75 | + /// <param name="dateTime">Given Date and Time</param> |
| 76 | + /// <returns>A new Uuid object</returns> |
| 77 | + public static Uuid NewUUIDv6(DateTime dateTime) |
| 78 | + { |
| 79 | + return NewUUIDv6(dateTime, GetClockSequence(), GetNodeID()); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Initialises a new GUID/UUID based on Version 6 (date-time), based on the given Node ID. |
| 84 | + /// </summary> |
| 85 | + /// <param name="nodeID">Given 48-bit Node ID</param> |
| 86 | + /// <returns>A new Uuid object</returns> |
| 87 | + /// <exception cref="ArgumentNullException"></exception> |
| 88 | + /// <exception cref="ArgumentException"></exception> |
| 89 | + public static Uuid NewUUIDv6(Byte[] nodeID) |
| 90 | + { |
| 91 | + return NewUUIDv6(DateTime.UtcNow, GetClockSequence(), nodeID); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Initialises a new GUID/UUID based on Version 6 (date-time), based on the given date and time, Clock Sequence with Variant and Node ID. |
| 96 | + /// </summary> |
| 97 | + /// <param name="dateTime">Given Date and Time</param> |
| 98 | + /// <param name="clockSequence">Given 16-bit Clock Sequence with Variant</param> |
| 99 | + /// <param name="nodeID">Given 48-bit Node ID</param> |
| 100 | + /// <returns>A new Uuid object</returns> |
| 101 | + /// <exception cref="ArgumentNullException"></exception> |
| 102 | + /// <exception cref="ArgumentException"></exception> |
| 103 | + public static Uuid NewUUIDv6(DateTime dateTime, Byte[] clockSequence, Byte[] nodeID) |
| 104 | + { |
| 105 | + if (clockSequence == null) |
| 106 | + throw new ArgumentNullException(nameof(clockSequence)); |
| 107 | + |
| 108 | + if (clockSequence.Length < 2) |
| 109 | + throw new ArgumentException(String.Format("Clock Sequence contains less than 16-bit: {0} bytes", clockSequence.Length), nameof(clockSequence)); |
| 110 | + |
| 111 | + if (nodeID == null) |
| 112 | + throw new ArgumentNullException(nameof(nodeID)); |
| 113 | + |
| 114 | + if (nodeID.Length < 6) |
| 115 | + throw new ArgumentException(String.Format("Node ID contains less than 48-bit: {0} bytes", nodeID.Length), nameof(nodeID)); |
| 116 | + |
| 117 | + TimeSpan timesince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime(); |
| 118 | + Int64 timeinterval = timesince.Ticks << 4; |
| 119 | + |
| 120 | + Byte[] time = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeinterval)); |
| 121 | + |
| 122 | + Byte[] hex = new Byte[16]; |
| 123 | + |
| 124 | + hex[0] = time[0]; |
| 125 | + hex[1] = time[1]; |
| 126 | + hex[2] = time[2]; |
| 127 | + hex[3] = time[3]; |
| 128 | + |
| 129 | + hex[4] = time[4]; |
| 130 | + hex[5] = time[5]; |
| 131 | + |
| 132 | + hex[6] = (Byte)(((time[6] >> 4) & 0x0F) + 0x60); |
| 133 | + hex[7] = (Byte)((time[6] << 4) + (time[7] >> 4)); |
| 134 | + |
| 135 | + hex[8] = clockSequence[0]; |
| 136 | + hex[9] = clockSequence[1]; |
| 137 | + |
| 138 | + hex[10] = nodeID[0]; |
| 139 | + hex[11] = nodeID[1]; |
| 140 | + hex[12] = nodeID[2]; |
| 141 | + hex[13] = nodeID[3]; |
| 142 | + hex[14] = nodeID[4]; |
| 143 | + hex[15] = nodeID[5]; |
| 144 | + |
| 145 | + Uuid Id = new Uuid(hex); |
| 146 | + |
| 147 | + return Id; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments