Device.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Net;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace RevokeMsgPatcher.Utils
  9. {
  10. public class Device
  11. {
  12. private static string macID = null;
  13. private static string osVersion = null;
  14. private static string fingerPrint = null;
  15. #region PROP, get it only once
  16. public static string MacID
  17. {
  18. get
  19. {
  20. if (macID == null)
  21. {
  22. macID = ObtainMacID();
  23. }
  24. return macID;
  25. }
  26. }
  27. public static string OSVersion
  28. {
  29. get
  30. {
  31. if (osVersion == null)
  32. {
  33. var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>()
  34. select x.GetPropertyValue("Caption")).FirstOrDefault();
  35. osVersion = name != null ? name.ToString() : "Unknown";
  36. }
  37. return osVersion;
  38. }
  39. }
  40. #endregion
  41. /// <summary>
  42. /// Calculate GUID
  43. /// </summary>
  44. /// <returns>GUID</returns>
  45. public static string Value()
  46. {
  47. if (fingerPrint == null)
  48. {
  49. fingerPrint = GetHash(
  50. "MAC >> " + MacID
  51. );
  52. }
  53. return fingerPrint;
  54. }
  55. private static string GetHash(string s)
  56. {
  57. MD5 sec = new MD5CryptoServiceProvider();
  58. ASCIIEncoding enc = new ASCIIEncoding();
  59. byte[] bt = enc.GetBytes(s);
  60. return GetHexString(sec.ComputeHash(bt));
  61. }
  62. private static string GetHexString(byte[] bt)
  63. {
  64. string s = string.Empty;
  65. for (int i = 0; i < bt.Length; i++)
  66. {
  67. byte b = bt[i];
  68. int n, n1, n2;
  69. n = (int)b;
  70. n1 = n & 15;
  71. n2 = (n >> 4) & 15;
  72. if (n2 > 9)
  73. s += ((char)(n2 - 10 + (int)'A')).ToString();
  74. else
  75. s += n2.ToString();
  76. if (n1 > 9)
  77. s += ((char)(n1 - 10 + (int)'A')).ToString();
  78. else
  79. s += n1.ToString();
  80. if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
  81. }
  82. return s;
  83. }
  84. #region Original Device ID Getting Code
  85. public static string ObtainMacID()
  86. {
  87. return Identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
  88. }
  89. private static string Identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
  90. {
  91. string result = "";
  92. try
  93. {
  94. ManagementClass mc = new ManagementClass(wmiClass);
  95. ManagementObjectCollection moc = mc.GetInstances();
  96. foreach (ManagementObject mo in moc)
  97. {
  98. if (mo[wmiMustBeTrue].ToString() == "True")
  99. {
  100. //Only get the first one
  101. if (result == "")
  102. {
  103. result = mo[wmiProperty].ToString();
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. catch
  110. {
  111. }
  112. return result;
  113. }
  114. private static string Identifier(string wmiClass, string wmiProperty)
  115. {
  116. string result = "";
  117. try
  118. {
  119. ManagementClass mc = new ManagementClass(wmiClass);
  120. ManagementObjectCollection moc = mc.GetInstances();
  121. foreach (ManagementObject mo in moc)
  122. {
  123. //Only get the first one
  124. if (result == "")
  125. {
  126. result = mo[wmiProperty].ToString();
  127. break;
  128. }
  129. }
  130. }
  131. catch
  132. {
  133. }
  134. return result;
  135. }
  136. #endregion
  137. }
  138. }