-
c#获取当前HASH的方法
基本原理
用C#实现在这个过程,我们需要进行下面几个步骤:
得到当前进程所对应的本地宿主文件;
打开这个文件流;
确定hash算法,计算文件流的hash;
将hash结果转换为我们熟悉的字符串表现形式。
下面就分别就这几个方面来进行解释。
获取宿主文件路径
在System.Diagnostics命名空间下,有个Process类,MSDN的描述是"提供对本地和远程进程的访问并使您能够启动和停止本地系统进程"。该类有一个静态方法GetCurrentProcess(),利用它我们可以获取当前进程。
Process类的MainModule属性包含了该进程所关联的主模块,换句话说也就是宿主文件,而MainModule的FileName属性,就是该宿主文件的完整路径。
ProcesscurrProcess=Process.GetCurrentProcess();
stringfilePath=currProcess.MainModule.FileName;
更多的关于获取当前路径和程序集的方法,可以参见C#获取当前路径的方法集合。
打开文件流
这个本来没什么好说的,直接用FileStream打开就行,但切记要将FileMode和FileAccess都设置成只读,否则可能会导致运行时错误。
using(FileStreamfs=newFileStream(filePath,FileMode.Open,FileAccess.Read))
{
//hash算法
fs.Close();
}
确定hash算法
这里我们用MD5算法为例。
.NET框架为提供了System.Security.Cryptography命名空间,我们使用其中的MD5类来进行hash计算。该类的下面几个成员需要我们注意:
Create()静态方法:生成一个MD5类的实例。
ComputeHash()方法:计算输入数据的哈希值。
在这里要注意,ComputeHash()所能接受的参数可以是Stream也可以是byte[],为了方便起见我们用前者。它的返回值却只能是byte[],长度固定为16。
MD5algorithm=MD5.Create();
byte[]hashData=algorithm.ComputeHash(fs);
Hash结果的表示
我们常用的MD5 hash结果都是以32个字符来表示的16进制串,因此需要对上面得到的byte[]进行转换。这个过程可以使用Convert.ToString(Int32, Int32)来进行十进制数向16进制转换的过程。也可以用更简单的byte.ToString("x2")来完成。
privatestaticstringByteArrayToHexString(byte[]bytes)
{
intlength=bytes.Length;
StringBuildersb=newStringBuilder();
foreach(bytedatainbytes)
{
sb.Append(data.ToString("x2"));
}
returnsb.ToString();
}
完整的代码
CurrentProcessHashTest
1usingSystem;
2usingSystem.Diagnostics;
3usingSystem.IO;
4usingSystem.Security.Cryptography;
5usingSystem.Text;
6
7namespaceNocturne.Samples.CurrentProcessHashTest
8{
9 classProgram
10 {
11 staticvoidMain(string[]args)
12 {
13 ProcesscurrProcess=Process.GetCurrentProcess();
14 stringfilePath=currProcess.MainModule.FileName;
15 stringhash=string.Empty;
16
17 using(FileStreamfs=newFileStream(filePath,FileMode.Open,FileAccess.Read))
18 {
19 MD5algorithm=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 privatestaticstringByteArrayToHexString(byte[]bytes)
34 {
35 intlength=bytes.Length;
36
37 StringBuildersb=newStringBuilder();
38
39 foreach(bytedatainbytes)
40 {
41 sb.Append(data.ToString("x2"));
42 }
43
44 returnsb.ToString();
45 }
46 }
47}
运行结果
由于Visual Studio的debug模式下,实际进程是一个.vshost.exe的文件,因此要验证程序的正确性,一定要先编译出可执行文件,然后手动去执行它,然后再与hash工具进行对比。
Debug运行的结果:
直接运行.exe的结果:
bindebug目录下的文件:
用hash工具计算的结果:
MD5algorithm=MD5.Create();
byte[]hashData=algorithm.ComputeHash(fs);
Hash结果的表示
我们常用的MD5 hash结果都是以32个字符来表示的16进制串,因此需要对上面得到的byte[]进行转换。这个过程可以使用Convert.ToString(Int32, Int32)来进行十进制数向16进制转换的过程。也可以用更简单的byte.ToString("x2")来完成。
privatestaticstringByteArrayToHexString(byte[]bytes)
{
intlength=bytes.Length;
StringBuildersb=newStringBuilder();
foreach(bytedatainbytes)
{
sb.Append(data.ToString("x2"));
}
returnsb.ToString();
}
完整的代码
CurrentProcessHashTest
1usingSystem;
2usingSystem.Diagnostics;
3usingSystem.IO;
4usingSystem.Security.Cryptography;
5usingSystem.Text;
6
7namespaceNocturne.Samples.CurrentProcessHashTest
8{
9 classProgram
10 {
11 staticvoidMain(string[]args)
12 {
13 ProcesscurrProcess=Process.GetCurrentProcess();
14 stringfilePath=currProcess.MainModule.FileName;
15 stringhash=string.Empty;
16
17 using(FileStreamfs=newFileStream(filePath,FileMode.Open,FileAccess.Read))
18 {
19 MD5algorithm=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 privatestaticstringByteArrayToHexString(byte[]bytes)
34 {
35 intlength=bytes.Length;
36
37 StringBuildersb=newStringBuilder();
38
39 foreach(bytedatainbytes)
40 {
41 sb.Append(data.ToString("x2"));
42 }
43
44 returnsb.ToString();
45 }
46 }
47}
运行结果
由于Visual Studio的debug模式下,实际进程是一个.vshost.exe的文件,因此要验证程序的正确性,一定要先编译出可执行文件,然后手动去执行它,然后再与hash工具进行对比。
Debug运行的结果:
直接运行.exe的结果:
bindebug目录下的文件:
用hash工具计算的结果: