using System.Collections.Generic;
public static partial class GuidGenerator
public const int ByteArraySize = 16;
public const int VariantByte = 8;
public const int VariantByteMask = 0x3f;
public const int VariantByteShift = 0x80;
public const int VersionByte = 7;
public const int VersionByteMask = 0x0f;
public const int VersionByteShift = 4;
private const byte TimestampByte = 0;
private const byte GuidClockSequenceByte = 8;
private const byte NodeByte = 10;
private static readonly DateTimeOffset GregorianCalendarStart = new DateTimeOffset(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);
public static byte[] DefaultClockSequence { get; set; }
public static byte[] DefaultNode { get; set; }
DefaultClockSequence = new byte[2];
DefaultNode = new byte[6];
var random = new Random();
random.NextBytes(DefaultClockSequence);
random.NextBytes(DefaultNode);
public static GuidVersion GetVersion(this Guid guid)
byte[] bytes = guid.ToByteArray();
return (GuidVersion)((bytes[VersionByte] & 0xFF) >> VersionByteShift);
public static DateTimeOffset GetDateTimeOffset(Guid guid)
byte[] bytes = guid.ToByteArray();
bytes[VersionByte] &= (byte)VersionByteMask;
bytes[VersionByte] |= (byte)((byte)GuidVersion.TimeBased >> VersionByteShift);
byte[] timestampBytes = new byte[8];
Array.Copy(bytes, TimestampByte, timestampBytes, 0, 8);
long timestamp = BitConverter.ToInt64(timestampBytes, 0);
long ticks = timestamp + GregorianCalendarStart.Ticks;
return new DateTimeOffset(ticks, TimeSpan.Zero);
public static DateTime GetDateTime(Guid guid)
return GetDateTimeOffset(guid).DateTime;
public static DateTime GetLocalDateTime(Guid guid)
return GetDateTimeOffset(guid).LocalDateTime;
public static DateTime GetUtcDateTime(Guid guid)
return GetDateTimeOffset(guid).UtcDateTime;
public static Guid GenerateTimeBasedGuid()
return GenerateTimeBasedGuid(DateTimeOffset.UtcNow, DefaultClockSequence, DefaultNode);
public static Guid GenerateTimeBasedGuid(DateTime dateTime)
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime)
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
public static Guid GenerateTimeBasedGuid(DateTime dateTime, byte[] clockSequence, byte[] node)
return GenerateTimeBasedGuid(new DateTimeOffset(dateTime), clockSequence, node);
public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime, byte[] clockSequence, byte[] node)
if (clockSequence == null)
throw new ArgumentNullException("clockSequence");
throw new ArgumentNullException("node");
if (clockSequence.Length != 2)
throw new ArgumentOutOfRangeException("clockSequence", "The clockSequence must be 2 bytes.");
throw new ArgumentOutOfRangeException("node", "The node must be 6 bytes.");
long ticks = (dateTime - GregorianCalendarStart).Ticks;
byte[] guid = new byte[ByteArraySize];
byte[] timestamp = BitConverter.GetBytes(ticks);
Array.Copy(node, 0, guid, NodeByte, Math.Min(6, node.Length));
Array.Copy(clockSequence, 0, guid, GuidClockSequenceByte, Math.Min(2, clockSequence.Length));
Array.Copy(timestamp, 0, guid, TimestampByte, Math.Min(8, timestamp.Length));
guid[VariantByte] &= (byte)VariantByteMask;
guid[VariantByte] |= (byte)VariantByteShift;
guid[VersionByte] &= (byte)VersionByteMask;
guid[VersionByte] |= (byte)((byte)GuidVersion.TimeBased << VersionByteShift);
public static void Main()
var msgs = new messages();
msgs.list = new List<MessageData>();
for (int i=0; i < 10; i++)
var msg = new MessageData()
timestamp = DateTime.UtcNow.ToString("s"),
content = "this a message"
string json = JsonConvert.SerializeObject(msgs, Formatting.Indented);
for (int i=0; i < 10; i++)
Array.Copy(Encoding.UTF8.GetBytes("Berkecan"), node, 6);
var clockseq = new byte[2];
var rndint = new Random().Next(0,9) + new Random().Next(0,9);
Array.Copy(BitConverter.GetBytes(rndint), clockseq, 2);
Console.WriteLine(GuidGenerator.GenerateTimeBasedGuid(new DateTime(552877848310000000), clockseq, node).ToString());
public static string RandomString(int length, int? seed = null)
string text = "abcdefghijklmnoprstuvyzxwq0123456789";
char[] array = (text).ToCharArray();
Random random = new Random( (seed == null ? Guid.NewGuid().GetHashCode() : seed.Value) );
string text2 = string.Empty;
for (int i = 0; i < length; i++)
text2 += array[random.Next(array.Length)].ToString();
public static class Extension
public static string Mask(this string value, string mask, char substituteChar = '#')
return new string(mask.Select(maskChar => maskChar == substituteChar ? value[valueIndex++] : maskChar).ToArray());
catch (IndexOutOfRangeException e)
throw new Exception("Value too short to substitute all substitute characters in the mask", e);
public List<MessageData> list {get;set;}
public string ID {get;set;}
public string username {get;set;}
public string timestamp {get;set;}
public string content {get;set;}