using System; using System.Security.Cryptography; using System.Text; namespace OS.Common.Encrypt { /// /// Sha1加密类 /// public static class Sha1 { /// /// 获取Sha1加密值 /// /// /// /// public static string Encrypt(string input, Encoding encoding = null) { if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input", "Sha1加密的字符串不能为空!"); if (encoding == null) encoding = Encoding.UTF8; var data = encoding.GetBytes(input); var encryData = Encrypt(data); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < encryData.Length; i++) { sBuilder.Append(encryData[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } /// /// 获取Sha1加密值 /// /// /// public static byte[] Encrypt(byte[] bytes) { if (bytes == null || bytes.Length == 0) throw new ArgumentNullException("bytes","Sha1加密的字节不能为空!"); using (SHA1 sha1Hash = SHA1.Create()) { return sha1Hash.ComputeHash(bytes); } } } }