[C#] SHA1加签名 →→→→→进入此内容的聊天室

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

回复 "SHA1加签名"

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

captcha