/// <summary> /// Combine CPU ID, Disk C Volume Serial Number and Motherboard Serial Number as device Id /// </summary> /// <param name="appName">Name of the application.</param> /// <returns>System.String.</returns> internal static string GenerateUID(string appName) { //Combine the IDs and get bytes var _id = $"{appName}.{GetProcessorId()}{GetMotherboardID()}"; var _byteIds = Encoding.UTF8.GetBytes(_id); //Use MD5 to get the fixed length checksum of the ID string var _md5 = new MD5CryptoServiceProvider(); var _checksum = _md5.ComputeHash(_byteIds); //Convert checksum into 4 ulong parts and use BASE36 to encode both var _part1Id = BASE36.Encode(BitConverter.ToUInt32(_checksum, 0)); var _part2Id = BASE36.Encode(BitConverter.ToUInt32(_checksum, 4)); var _part3Id = BASE36.Encode(BitConverter.ToUInt32(_checksum, 8)); var _part4Id = BASE36.Encode(BitConverter.ToUInt32(_checksum, 12)); //Concat these 4 part into one string return($"{_part1Id}-{_part2Id}-{_part3Id}-{_part4Id}".Substring(0, 28)); }
/// <summary> /// Gets the uid in bytes. /// </summary> /// <param name="UID">The uid.</param> /// <returns>System.Byte[].</returns> /// <exception cref="ArgumentException">Wrong UID</exception> /// <exception cref="System.ArgumentException">Wrong UID</exception> internal static byte[] GetUIDInBytes(string UID) { //Split 4 part Id into 4 ulong var _ids = UID.Split('-'); if (_ids.Length != 4) { throw new ArgumentException("Wrong UID"); } //Combine 4 part Id into one byte array var _value = new byte[16]; Buffer.BlockCopy(BitConverter.GetBytes(BASE36.Decode(_ids[0])), 0, _value, 0, 8); Buffer.BlockCopy(BitConverter.GetBytes(BASE36.Decode(_ids[1])), 0, _value, 8, 8); Buffer.BlockCopy(BitConverter.GetBytes(BASE36.Decode(_ids[2])), 0, _value, 16, 8); Buffer.BlockCopy(BitConverter.GetBytes(BASE36.Decode(_ids[3])), 0, _value, 24, 8); return(_value); }