using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
/// <summary>
/// MyHash 的摘要说明
/// </summary>
public class MyHash
{
public MyHash()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 获取一个文件的Hah值
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetHashByFilePath(string path)
{
try
{
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Security.Cryptography.MD5CryptoServiceProvider md5Code = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = md5Code.ComputeHash(file);
file.Close();
string res = System.BitConverter.ToString(hash_byte);
res = res.Replace("-", "");
return res;
}
catch (Exception e)
{
return "";
}
}
/// <summary>
/// 获取一个文件流的Hah值
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static string GetHashByStream(Stream stream)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider md5Code = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = md5Code.ComputeHash(stream);
string res = System.BitConverter.ToString(hash_byte);
res = res.Replace("-", "");
return res;
}
catch (Exception e)
{
return "";
}
}
}
原创的免费软件 - LoopcVPN,DnsAcc,DNS加速器,百度相册助手