Basic Principles
In this process, we need to perform the following steps:
- Obtain the local host file corresponding to the current process;
- Open the file stream;
- Determine the hash algorithm to calculate the hash of the file stream;
- Convert the hash result to a string representation that we are familiar.
The following sections describe these aspects respectively.
Obtain the host file path
There is a Process class in the System. Diagnostics namespace. MSDN describes "access to local and remote processes and enabling you to start and stop local System processes ". This class has a static method GetCurrentProcess (), which we can use to obtain the current process.
The MainModule attribute of the Process class contains the main module associated with the Process, that is, the host file, and the FileName attribute of the MainModule is the complete path of the host file.
Process currProcess = Process. GetCurrentProcess ();
String filePath = currProcess. MainModule. FileName;
For more information about how to obtain the current path and assembly, see C # How to obtain the current path.
Open file stream
There is nothing to say about this. Simply use FileStream to open it, but remember to set both FileMode and FileAccess to read-only; otherwise, it may cause a running error.
Using (FileStream fs = new FileStream (filePath, FileMode. Open, FileAccess. Read ))
{
// Hash Algorithm
Fs. Close ();
}
Determine the hash algorithm
Here we use the MD5 algorithm as an example.
The. NET Framework provides the System. Security. Cryptography namespace. We use the MD5 class for hash calculation. Note the following:
- Create () Static Method: generate an MD5 class instance.
- ComputeHash () method: Calculate the hash value of input data.
Note that the parameter accepted by ComputeHash () can be Stream or byte []. We use the former for convenience. Its return value can only be byte [], with a fixed length of 16.
MD5 algorithm = MD5.Create ();
Byte [] hashData = algorithm. ComputeHash (fs );
Hash result Representation
The commonly used MD5 hash results are all hexadecimal strings expressed by 32 characters. Therefore, we need to convert the byte [] obtained above. In this process, Convert. ToString (Int32, Int32) can be used to Convert the decimal number to hexadecimal number. You can also use a simpler byte. ToString ("x2.
Private static string ByteArrayToHexString (byte [] bytes)
{
Int length = bytes. Length;
StringBuilder sb = new StringBuilder ();
Foreach (byte data in bytes)
{
Sb. Append (data. ToString ("x2 "));
}
Return sb. ToString ();
}
Complete code
CurrentProcessHashTest
1 using System;
2 using System. Diagnostics;
3 using System. IO;
4 using System. Security. Cryptography;
5 using System. Text;
6
7 namespace Nocturne. Samples. CurrentProcessHashTest
8 {
9 class Program
10 {
11 static void Main (string [] args)
12 {
13 Process currProcess = Process. GetCurrentProcess ();
14 string filePath = currProcess. MainModule. FileName;
15 string hash = string. Empty;
16
17 using (FileStream fs = new FileStream (filePath, FileMode. Open, FileAccess. Read ))
18 {
19 MD5 algorithm = MD5.Create ();
20
21 byte [] hashData = algorithm. ComputeHash (fs );
22
23 hash = ByteArrayToHexString (hashData );
24
25 fs. Close ();
26}
27
28 Console. WriteLine ("Hash:" + hash. ToString ());
29
30 Console. ReadKey ();
31}
32
33 private static string ByteArrayToHexString (byte [] bytes)
34 {
35 int length = bytes. Length;
36
37 StringBuilder sb = new StringBuilder ();
38
39 foreach (byte data in bytes)
40 {
41 sb. Append (data. ToString ("x2 "));
42}
43
44 return sb. ToString ();
45}
46}
47} running result
In the debug format of Visual Studio, the upload process is a .vshost.exe file. Therefore, to verify the correctness of the program, you must compile the executable file, execute it manually, and then compare it with the hash tool.
Debug running result:
Run the result of .exe directly:
Files in the bin \ debug directory:
Result calculated using the hash tool: