發(fā)布時(shí)間:2020-08-06 15:16 作者:獨(dú)孤劍 閱讀:2511
SHA1加密在軟件開發(fā)中經(jīng)常使用, 由于是單向加密也稱作不可逆加密, 經(jīng)常用來做為校驗(yàn)使用, 可以用來校驗(yàn)用戶密碼, 文件校驗(yàn)
/// <summary> /// SHA1字符串加密 /// </summary> /// <param name="input">需要加密字符串</param> /// <returns>返回40位大寫字符串</returns> public static string SHA1(string input) { try { using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) { string result = ""; byte[] btInput = System.Text.Encoding.UTF8.GetBytes(input); byte[] btOut = sha1.ComputeHash(btInput); for (int i = 0; i < btOut.Length; i++) { result += btOut[i].ToString("X2"); } return result; } } catch { throw; } } /// <summary> /// SHA1文件流加密 /// </summary> /// <param name="input">文件流</param> /// <returns>返回40位大寫字符串</returns> public static string SHA1(Stream input) { try { using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) { string result = ""; byte[] btOut = sha1.ComputeHash(input); for (int i = 0; i < btOut.Length; i++) { result += btOut[i].ToString("X2"); } return result; } } catch { throw; } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對(duì)您有幫助,歡迎給作者打賞