日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

计算hash值

發布時間:2024/1/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算hash值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class 計算hash值{//計算hash值需要引用系統包:using System.Security.Cryptography;//計算分兩步://1. 計算hash: MD5 calculator = MD5.Create(); // Byte[] buffer = calculator.ComputeHash(fs);==>計算完成后需要釋放(1.使用using 2.完成后執行calculator.Clear();)//2. 將字節數組轉換成十六進制的字符串形式public void ComputeHash(){string hashMD5 = string.Empty;//讀取文件流using (FileStream fs = new FileStream(@"C:\Users\lu.jiang.UNITED-IMAGING\Desktop\ReadMe.txt", FileMode.Open, FileAccess.Read)){System.Security.Cryptography.MD5 calculator = System.Security.Cryptography.MD5.Create();Byte[] buffer = calculator.ComputeHash(fs);calculator.Clear();//將字節數組轉換成十六進制的字符串形式StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < buffer.Length; i++){stringBuilder.Append(buffer[i].ToString("x2"));//10和26轉換為16進制二位數后為:0a 1A}hashMD5 = stringBuilder.ToString();}Console.WriteLine(hashMD5);Console.ReadKey();}//注意:如果輸入為字符串,計算字符串input的hash,需要先轉換為字節//Encoding.UTF8.GetBytes(input)/// <summary>/// 計算指定文件的CRC32值/// </summary>/// <param name="fileName">指定文件的完全限定名稱</param>/// <returns>返回值的字符串形式</returns>public static String ComputeCRC32(String fileName){String hashCRC32 = String.Empty;//檢查文件是否存在,如果文件存在則進行計算,否則返回空值if (System.IO.File.Exists(fileName)){using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)){//計算文件的CSC32值Crc32 calculator = new Crc32();Byte[] buffer = calculator.ComputeHash(fs);calculator.Clear();//將字節數組轉換成十六進制的字符串形式StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < buffer.Length; i++){stringBuilder.Append(buffer[i].ToString("x2"));}hashCRC32 = stringBuilder.ToString();}//關閉文件流}return hashCRC32;}//ComputeCRC32/// <summary>/// 計算指定文件的SHA1值/// </summary>/// <param name="fileName">指定文件的完全限定名稱</param>/// <returns>返回值的字符串形式</returns>public static String ComputeSHA1(String fileName){String hashSHA1 = String.Empty;//檢查文件是否存在,如果文件存在則進行計算,否則返回空值if (System.IO.File.Exists(fileName)){using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)){//計算文件的SHA1值System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();Byte[] buffer = calculator.ComputeHash(fs);calculator.Clear();//將字節數組轉換成十六進制的字符串形式StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < buffer.Length; i++){stringBuilder.Append(buffer[i].ToString("x2"));}hashSHA1 = stringBuilder.ToString();}//關閉文件流}return hashSHA1;}//ComputeSHA1}//end class: HashHelper/// <summary>/// 提供 CRC32 算法的實現/// </summary>public class Crc32 : System.Security.Cryptography.HashAlgorithm{public const UInt32 DefaultPolynomial = 0xedb88320;public const UInt32 DefaultSeed = 0xffffffff;private UInt32 hash;private UInt32 seed;private UInt32[] table;private static UInt32[] defaultTable;public Crc32(){table = InitializeTable(DefaultPolynomial);seed = DefaultSeed;Initialize();}public Crc32(UInt32 polynomial, UInt32 seed){table = InitializeTable(polynomial);this.seed = seed;Initialize();}public override void Initialize(){hash = seed;}protected override void HashCore(byte[] buffer, int start, int length){hash = CalculateHash(table, hash, buffer, start, length);}protected override byte[] HashFinal(){byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);this.HashValue = hashBuffer;return hashBuffer;}public static UInt32 Compute(byte[] buffer){return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);}public static UInt32 Compute(UInt32 seed, byte[] buffer){return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);}public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer){return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);}private static UInt32[] InitializeTable(UInt32 polynomial){if (polynomial == DefaultPolynomial && defaultTable != null){return defaultTable;}UInt32[] createTable = new UInt32[256];for (int i = 0; i < 256; i++){UInt32 entry = (UInt32)i;for (int j = 0; j < 8; j++){if ((entry & 1) == 1)entry = (entry >> 1) ^ polynomial;elseentry = entry >> 1;}createTable[i] = entry;}if (polynomial == DefaultPolynomial){defaultTable = createTable;}return createTable;}private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size){UInt32 crc = seed;for (int i = start; i < size; i++){unchecked{crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];}}return crc;}private byte[] UInt32ToBigEndianBytes(UInt32 x){return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) };}}

?

總結

以上是生活随笔為你收集整理的计算hash值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。