C # method to get current process hash

Source: Internet
Author: User
Tags hash md5

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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.