Skip to content

Instantly share code, notes, and snippets.

@QiMata
Created June 26, 2020 12:25
Show Gist options
  • Save QiMata/652443148563dbdc32787728a80d0f9c to your computer and use it in GitHub Desktop.
Save QiMata/652443148563dbdc32787728a80d0f9c to your computer and use it in GitHub Desktop.
Creating a new guid using the channel info
enum Channel : byte
{
UnknownChannel = 0,
Email = 1,
CallCenter = 2,
BrokerHelpDesk = 3,
BrokerPortal = 4
}
class GuidGenerator
{
private const int SwissReId = 1234;
private readonly Random _random = new Random();
private readonly MD5 _md5Hasher = MD5.Create();
public Guid CreateSwissReGuid(Channel channel, int channelId)
{
byte[] channelIdBytes = BitConverter.GetBytes(channelId);
return new Guid(SwissReId, (short)_random.Next(), (short)_random.Next(),
(byte)_random.Next(), (byte)_random.Next(), (byte)_random.Next(),(byte)channel,
channelIdBytes[0],channelIdBytes[1],channelIdBytes[2],channelIdBytes[3]);
}
public Guid CreateSwissReGuid(Channel channel, string channelId)
{
var hashed = _md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(channelId));
return CreateSwissReGuid(channel, BitConverter.ToInt32(hashed, 0));
}
}
class Program
{
static void Main(string[] args)
{
List<Guid> guidCheck = new List<Guid>(100);
GuidGenerator generator = new GuidGenerator();
for (int i = 0; i < 100; i++)
{
var swissReGuid = generator.CreateSwissReGuid(Channel.Email, DateTime.Now.GetHashCode());
guidCheck.Add(swissReGuid);
Console.WriteLine($"Generated new guid: {swissReGuid}");
Task.Delay(100).Wait();
}
Console.WriteLine($"Created {guidCheck.Distinct().Count()} unique guids!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment