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
class md5
{
/// <summary>
/// 实现对一个文件md5的读取,path为文件路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string md5_hash(string path)
{
try
{
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
string resule = System.BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
return resule;
}
catch (Exception e)
{

return e.ToString();

}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


public static void getFileMD5(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5byte = md5.ComputeHash(fs);
int i, j;
foreach (byte b in md5byte)
{
i = Convert.ToInt32(b);
j = i >> 4;
Console.Write(Convert.ToString(j, 16));
j = ((i << 4) & 0x00ff) >> 4;
Console.Write(Convert.ToString(j, 16));
}
Console.ReadLine();

}

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
73
74
75
76
77
78
79
public enum HashType
{
MD5 = 0,
SHA1=1
}
public class Md5Checker
{
/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fileName">要计算 MD5 值的文件名和路径</param>
/// <returns>MD5 值16进制字符串</returns>
public static string MD5File(string fileName)
{
return HashFile(fileName, HashType.MD5);
}

/// <summary>
/// 计算文件的 SHA1 值
/// </summary>
/// <param name="fileName">要计算 SHA1 值的文件名和路径</param>
/// <returns>SHA1 值16进制字符串</returns>
public static string SHA1File(string fileName)
{
return HashFile(fileName, HashType.SHA1);
}
/// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fileName">要计算哈希值的文件名和路径</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值16进制字符串</returns>
public static string HashFile(string fileName, HashType hashType)
{
if (!System.IO.File.Exists(fileName))
return string.Empty;

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
byte[] hashBytes = HashData(fs, hashType);
fs.Close();
return ByteArrayToHexString(hashBytes);
}
/// <summary>
/// 计算哈希值
/// </summary>
/// <param name="stream">要计算哈希值的 Stream</param>
/// <param name="hashType">算法:sha1,md5</param>
/// <returns>哈希值字节数组</returns>
public static byte[] HashData(Stream stream, HashType hashType)
{
HashAlgorithm algorithm = null;
switch (hashType)
{
case HashType.MD5:
algorithm = MD5.Create();
break;
case HashType.SHA1:
algorithm = SHA1.Create();
break;
default:
break;
}
return algorithm.ComputeHash(stream);
}
/// <summary>
/// 字节数组转换为16进制表示的字符串
/// </summary>
public static string ByteArrayToHexString(byte[] buf)
{
int iLen = 0;

// 通过反射获取 MachineKeySection 中的 ByteArrayToHexString 方法,该方法用于将字节数组转换为16进制表示的字符串。
Type type = typeof(System.Web.Configuration.MachineKeySection);
MethodInfo byteArrayToHexString = type.GetMethod("ByteArrayToHexString", BindingFlags.Static | BindingFlags.NonPublic);

// 字节数组转换为16进制表示的字符串
return (string)byteArrayToHexString.Invoke(null, new object[] { buf, iLen });
}
}