-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComputerID.cs
72 lines (61 loc) · 1.98 KB
/
ComputerID.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Diagnostics;
namespace SpecialFNs
{
class ComputerID
{
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool GetVolumeNameForVolumeMountPoint(string lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName, UInt32 cchBufferLength);
/// <summary>
/// Returns Volume GUID for a specified volume
/// </summary>
/// <param name="a_Path">Path to any file on the volume</param>
/// <returns>Volume GUID</returns>
public static string GetVolumeGuid(string a_Path)
{
string mountPoint = a_Path.Substring(0, 3);
StringBuilder buffer = new StringBuilder(128);
if (!GetVolumeNameForVolumeMountPoint(mountPoint, buffer, (UInt32)buffer.Capacity))
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return buffer.ToString();
}
/// <summary>
/// Returns Volume GUID for system volume
/// </summary>
/// <returns>System Volume GUID</returns>
public static string GetSystemVolumeGuid()
{
string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
return GetVolumeGuid(systemPath);
}
/// <summary>
/// Returns my variant of computer id (based on system Volume GUID)
/// This id consists of 2 md5 hashes: first is actual id, and second is the hash of first
/// </summary>
/// <returns>ComputerID string</returns>
public static string GetComputerID()
{
string volumeGUID = "";
try
{
volumeGUID = ComputerID.GetSystemVolumeGuid();
}
catch (System.Exception a_Exception)
{
Debug.Assert(false, a_Exception.Message);
}
byte[] hash = Utility.Md5String(volumeGUID);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
for (int i = 0; i < 64; i++)
{
hash = md5.ComputeHash(hash);
}
string leftPart = Utility.BytesToHex(hash);
string rightPart = Utility.BytesToHex(Utility.Md5String(leftPart));
return leftPart + rightPart;
}
}
}