一、计算文件
public static string HashCode( string str) |
{ |
string rethash = "" ; |
try |
{ |
System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); |
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); |
byte [] combined = encoder.GetBytes(str); |
hash.ComputeHash(combined); |
rethash = Convert.ToBase64String(hash.Hash); |
} |
catch (Exception ex) |
{ |
string strerr = "Error in HashCode : " + ex.Message; |
} |
return rethash; |
} |
using System; |
namespace myMethod |
{ |
class computeMD5andSHA1 |
{ |
/// <summary> |
/// 计算文件的 MD5 值 |
/// </summary> |
/// <param name="fileName">要计算 MD5 值的文件名和路径</param> |
/// <returns>MD5 值16进制字符串</returns> |
public string MD5File( string fileName) |
{ |
return HashFile(fileName , "md5" ); |
} |
/// <summary> |
/// 计算文件的 sha1 值 |
/// </summary> |
/// <param name="fileName">要计算 sha1 值的文件名和路径</param> |
/// <returns>sha1 值16进制字符串</returns> |
public string SHA1File( string fileName) |
{ |
return HashFile(fileName , "sha1" ); |
} |
/// <summary> |
/// 计算文件的哈希值 |
/// </summary> |
/// <param name="fileName">要计算哈希值的文件名和路径</param> |
/// <param name="algName">算法:sha1,md5</param> |
/// <returns>哈希值16进制字符串</returns> |
private string HashFile( string fileName , string algName) |
{ |
if ( !System.IO.File.Exists(fileName) ) |
return string .Empty; |
System.IO.FileStream fs = new System.IO.FileStream(fileName , System.IO.FileMode.Open , System.IO.FileAccess.Read); |
byte [] hashBytes = HashData(fs , algName); |
fs.Close(); |
return ByteArrayToHexString(hashBytes); |
} |
/// <summary> |
/// 计算哈希值 |
/// </summary> |
/// <param name="stream">要计算哈希值的 Stream</param> |
/// <param name="algName">算法:sha1,md5</param> |
/// <returns>哈希值字节数组</returns> |
private byte [] HashData(System.IO.Stream stream , string algName) |
{ |
System.Security.Cryptography.HashAlgorithm algorithm; |
if ( algName == null ) |
{ |
throw new ArgumentNullException( "algName 不能为 null" ); |
} |
if ( string .Compare(algName , "sha1" , true ) == 0 ) |
{ |
algorithm = System.Security.Cryptography.SHA1.Create(); |
} |
else |
{ |
if ( string .Compare(algName , "md5" , true ) != 0 ) |
{ |
throw new Exception( "algName 只能使用 sha1 或 md5" ); |
} |
algorithm = System.Security.Cryptography.MD5.Create(); |
} |
return algorithm.ComputeHash(stream); |
} |
/// <summary> |
/// 字节数组转换为16进制表示的字符串 |
/// </summary> |
private string ByteArrayToHexString( byte [] buf) |
{ |
return BitConverter.ToString(buf).Replace( "-" , "" ); |
} |
} |
} |
二、计算文本
- /// <summary>
- /// SHA1 加密,返回大写字符串
- /// </summary>
- /// <param name="content">需要加密字符串</param>
- /// <returns>返回40位UTF8 大写</returns>
- public static string SHA1(string content)
- {
- return SHA1(content, Encoding.UTF8);
- }
- /// <summary>
- /// SHA1 加密,返回大写字符串
- /// </summary>
- /// <param name="content">需要加密字符串</param>
- /// <param name="encode">指定加密编码</param>
- /// <returns>返回40位大写字符串</returns>
- public static string SHA1(string content, Encoding encode)
- {
- try
- {
- SHA1 sha1 = new SHA1CryptoServiceProvider();
- byte[] bytes_in = encode.GetBytes(content);
- byte[] bytes_out = sha1.ComputeHash(bytes_in);
- sha1.Dispose();
- string result = BitConverter.ToString(bytes_out);
- result = result.Replace("-", "");
- return result;
- }
- catch (Exception ex)
- {
- throw new Exception("SHA1加密出错:" + ex.Message);
- }
- }
三、加密文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class A { static string GetPwd( string Pwd) { byte [] data = System.Text.Encoding.Default.GetBytes(Pwd); //以字节方式存储 System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte [] result = sha1.ComputeHash(data); //得到哈希值 return System.BitConverter.ToString(result).Replace( "-" , "" ); //转换成为字符串的显示 } static void Main() { string input = "ABCD" ; string Pwd1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "SHA1" ); System.Console.WriteLine(Pwd1); string Pwd2 = GetPwd(input); System.Console.WriteLine(Pwd2); } } /* 程序输出: FB2F85C88567F3C8CE9B799C7C54642D0C7B41F6 FB2F85C88567F3C8CE9B799C7C54642D0C7B41F6 */ |