using System.Security.Cryptography;
public static readonly Guid StockNamespace = new Guid("a305265f-9b07-4204-9eec-f674bd7d7471");
public static void Main()
Console.WriteLine(CreateStockIdentity("NX18XUY",Guid.Parse("a6a3e838-dce4-4fa1-bda8-5114f89a42f0")));
public static Guid CreateStockIdentity(string vrm, Guid dealerId)
return Create(StockNamespace, vrm + dealerId);
public static Guid Create(Guid namespaceId, string name)
return Create(namespaceId, name, 6);
public static Guid Create(Guid namespaceId, string name, int version)
throw new ArgumentNullException("name");
if (version != 3 && version != 5 && version != 6)
throw new ArgumentOutOfRangeException("version", "version must be either 3 (md5) or 5 (sha1), or 6 (sha256).");
byte[] nameBytes = Encoding.UTF8.GetBytes(name);
byte[] namespaceBytes = namespaceId.ToByteArray();
SwapByteOrder(namespaceBytes);
using (var incrementalHash = version == 3 ? IncrementalHash.CreateHash(HashAlgorithmName.MD5) : version == 5 ? IncrementalHash.CreateHash(HashAlgorithmName.SHA1) : IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
incrementalHash.AppendData(namespaceBytes);
incrementalHash.AppendData(nameBytes);
hash = incrementalHash.GetHashAndReset();
byte[] newGuid = new byte[16];
Array.Copy(hash, 0, newGuid, 0, 16);
newGuid[6] = (byte)((newGuid[6] & 0x0F) | (version << 4));
newGuid[8] = (byte)((newGuid[8] & 0x3F) | 0x80);
return new Guid(newGuid);
private static void SwapByteOrder(byte[] guid)
private static void SwapBytes(byte[] guid, int left, int right)
guid[left] = guid[right];