/// <summary> /// Generates a RFC4122 random number based UUID (version-4) /// </summary> /// <returns></returns> public Guid GenerateGuid() { var data = new byte[16]; generator.GetBytes(data); data.AddVariantMarker().AddVersionMarker(UUIDVersion.Random); return(new Guid(data)); }
private static Guid GenerateGuid(DateTimeOffset dateTime, byte[] clockSequenceData, byte[] macAddressBytes) { if (macAddressBytes == null) { throw new ArgumentNullException(nameof(macAddressBytes)); } if (macAddressBytes.Length != 6) { throw new ArgumentException($"{nameof(macAddressBytes)} must have 6 bytes."); } if (clockSequenceData == null) { throw new ArgumentNullException(nameof(clockSequenceData)); } if (clockSequenceData.Length != 2) { throw new ArgumentException($"{nameof(clockSequenceData)} must have 2 bytes."); } var ticksSinceStart = (dateTime - ClockStart).Ticks; var timestampBytes = BitConverter.GetBytes(ticksSinceStart); var data = new byte[16]; /* * - Set the time_low field equal to the least significant 32 bits (bits zero through 31) of the timestamp in the same order of significance. * - Set the time_mid field equal to bits 32 through 47 from the timestamp in the same order of significance. * - Set the 12 least significant bits (bits zero through 11) of the time_hi_and_version field equal to bits 48 through 59 from the timestamp in the same order of significance. */ Array.Copy(timestampBytes, 0, data, 0, Math.Min(timestampBytes.Length, 8)); /* * - Set the clock_seq_low field to the eight least significant bits (bits zero through 7) of the clock sequence in the same order of significance. * - Set the 6 least significant bits (bits zero through 5) of the clock_seq_hi_and_reserved field to the 6 most significant bits (bits 8 through 13) of the clock sequence in the same order of significance. */ Array.Copy(clockSequenceData, 0, data, 8, clockSequenceData.Length); /* * - Set the node field to the 48-bit IEEE address in the same order of significance as the address. */ Array.Copy(macAddressBytes, 0, data, 10, macAddressBytes.Length); data.AddVariantMarker().AddVersionMarker(UUIDVersion.TimeBased); return(new Guid(data)); }