[C#] c# 计算文件的哈希值(MD5 sha1) →→→→→进入此内容的聊天室

来自 , 2021-01-12, 写在 C#, 查看 169 次.
URL http://www.code666.cn/view/e6acf4b0
  1. using System;
  2. namespace myMethod
  3. {
  4. class computeMD5andSHA1
  5. {
  6.         /// <summary>
  7.         /// 计算文件的 MD5 值
  8.         /// </summary>
  9.         /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
  10.         /// <returns>MD5 值16进制字符串</returns>
  11.         public string MD5File ( string fileName )
  12.         {
  13.                 return HashFile ( fileName , "md5" );
  14.         }
  15.  
  16.         /// <summary>
  17.         /// 计算文件的 sha1 值
  18.         /// </summary>
  19.         /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
  20.         /// <returns>sha1 值16进制字符串</returns>
  21.         public string SHA1File ( string fileName )
  22.         {
  23.                 return HashFile ( fileName , "sha1" );
  24.         }
  25.  
  26.  
  27.         /// <summary>
  28.         /// 计算文件的哈希值
  29.         /// </summary>
  30.         /// <param name="fileName">要计算哈希值的文件名和路径</param>
  31.         /// <param name="algName">算法:sha1,md5</param>
  32.         /// <returns>哈希值16进制字符串</returns>
  33.         private string HashFile ( string fileName , string algName )
  34.         {
  35.                 if ( !System.IO.File.Exists ( fileName ) )
  36.                         return string.Empty;
  37.  
  38.                 System.IO.FileStream fs = new System.IO.FileStream ( fileName , System.IO.FileMode.Open , System.IO.FileAccess.Read );
  39.                 byte[] hashBytes = HashData ( fs , algName );
  40.                 fs.Close();
  41.                 return ByteArrayToHexString ( hashBytes );
  42.         }
  43.  
  44.         /// <summary>
  45.         /// 计算哈希值
  46.         /// </summary>
  47.         /// <param name="stream">要计算哈希值的 Stream</param>
  48.         /// <param name="algName">算法:sha1,md5</param>
  49.         /// <returns>哈希值字节数组</returns>
  50.         private byte[] HashData ( System.IO.Stream stream , string algName )
  51.         {
  52.                 System.Security.Cryptography.HashAlgorithm algorithm;
  53.                 if ( algName == null )
  54.                 {
  55.                         throw new ArgumentNullException ( "algName 不能为 null" );
  56.                 }
  57.                 if ( string.Compare ( algName , "sha1" , true ) == 0 )
  58.                 {
  59.                         algorithm = System.Security.Cryptography.SHA1.Create();
  60.                 }
  61.                 else
  62.                 {
  63.                         if ( string.Compare ( algName , "md5" , true ) != 0 )
  64.                         {
  65.                                 throw new Exception ( "algName 只能使用 sha1 或 md5" );
  66.                         }
  67.                         algorithm = System.Security.Cryptography.MD5.Create();
  68.                 }
  69.                 return algorithm.ComputeHash ( stream );
  70.         }
  71.  
  72.         /// <summary>
  73.         /// 字节数组转换为16进制表示的字符串
  74.         /// </summary>
  75.         private string ByteArrayToHexString ( byte[] buf )
  76.         {
  77.                 return BitConverter.ToString ( buf ).Replace ( "-" , "" );
  78.         }
  79. }
  80. }
  81.  

回复 "c# 计算文件的哈希值(MD5 sha1)"

这儿你可以回复上面这条便签

captcha