Basic principle
In C # Implementation in this process, we need to do the following steps:
Gets the local host file corresponding to the current process;
Open this file stream;
The hash algorithm is determined to compute the hash of the file stream;
Converts the hash result to our familiar string representation.
Here are the explanations for each of these aspects.
Get the Host file path
Under the System.Diagnostics namespace, there is a process class, described in MSDN as "providing access to local and remote processes and enabling you to start and stop Local system processes." The class has a static method GetCurrentProcess () that allows us to get the current process.
The Mainmodule property of the Process class contains the main module associated with the procedure, in other words the host file, and the FileName property of the Mainmodule is the full path of the host file.
Process currprocess = process.getcurrentprocess ();
string filePath = CurrProcess.MainModule.FileName;
For more information about getting the current path and assembly, see C # to get a collection of methods for the current path.
Open File stream
This has nothing to say, directly with FileStream open on the line, but remember to set the FileMode and FileAccess are read-only, otherwise it may cause a run-time error.
using (FileStream fs = new FileStream (FilePath, FileMode.Open, FileAccess.Read))
{
Hash algorithm
Fs. Close ();
}
Determine hash algorithm
Here we use the MD5 algorithm as an example.
. NET Framework to provide the System.Security.Cryptography namespace, we use the MD5 class to perform hash calculations. The next few members of the class need our attention:
Create () static method: Generates an instance of a MD5 class.
ComputeHash () Method: Calculates the hash value of the input data.
Note here that ComputeHash () can accept parameters that can be either stream or byte[], and for convenience we use the former. Its return value is only byte[], with a fixed length of 16.
MD5 algorithm = MD5. Create ();
byte[] Hashdata = Algorithm.computehash (FS);