/// <summary> /// 计算SHA-256码 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public string Hash_SHA_256(string word, bool toUpper = true) { try { byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); System.Security.Cryptography.SHA256CryptoServiceProvider SHA256 = new System.Security.Cryptography.SHA256CryptoServiceProvider(); byte[] bytHash = SHA256.ComputeHash(bytValue); SHA256.Clear(); StringBuilder sb = new StringBuilder(); foreach (var b in bytHash) { sb.Append(b.ToString("X2")); } string sHash = sb.ToString(); #region //根据计算得到的Hash码翻译为SHA-1码 /*string sHash = "", sTemp = ""; * for (int counter = 0; counter < bytHash.Count(); counter++) * { * long i = bytHash[counter] / 16; * if (i > 9) * { * sTemp = ((char)(i - 10 + 0x41)).ToString(); * } * else * { * sTemp = ((char)(i + 0x30)).ToString(); * } * i = bytHash[counter] % 16; * if (i > 9) * { * sTemp += ((char)(i - 10 + 0x41)).ToString(); * } * else * { * sTemp += ((char)(i + 0x30)).ToString(); * } * sHash += sTemp; * }*/ #endregion //根据大小写规则决定返回的字符串 return(toUpper ? sHash : sHash.ToLower()); } catch (Exception ex) { throw new Exception(ex.Message); } }
public string EncryptSHA256(string textToEncrypt) { HashAlgorithm hasher = null; try { hasher = new SHA256Managed(); } catch { hasher = new SHA256CryptoServiceProvider(); } byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(textToEncrypt); byte[] hashedBytes = hasher.ComputeHash(plainBytes); hasher.Clear(); return Convert.ToBase64String(hashedBytes); }
/// <summary> /// 获取文本SHA256 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetTextSHA_256(string text) { if (string.IsNullOrEmpty(text)) { return(""); } System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP = new System.Security.Cryptography.SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(text); byte[] bytHash = SHA256CSP.ComputeHash(bytValue); SHA256CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码 string sHash = ConvertHashBytes(bytHash); //根据大小写规则决定返回的字符串 return(sHash.ToLower()); }
/// <summary> /// 计算SHA-256码 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string Hash_SHA_256(string word, bool toUpper = true) { try { System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP = new System.Security.Cryptography.SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA256CSP.ComputeHash(bytValue); SHA256CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写规则决定返回的字符串 return(toUpper ? sHash : sHash.ToLower()); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary>Get hash code</summary> /// <param name="value">value to be hashed</param> /// <returns>Hashed value</returns> private static string GetHash(string value) { byte[] hash = null; try { using (HashAlgorithm algorithm = new SHA256CryptoServiceProvider()) { if (value.Length > HashModifier.Length) value += ReverseString(HashModifier); else value += ReverseString(HashModifier.Substring(1, value.Length)); hash = Encoding.Unicode.GetBytes(value); for (var i = 0; i < 1000; i++) { hash = algorithm.ComputeHash(hash); } algorithm.Clear(); } } catch (Exception ex) { var message = ex.Message; } var hashed = StripCharacters(Convert.ToBase64String(hash)); return hashed; }
private NewPassword HashPassword(NewPassword np) { HashAlgorithm hashAlg = null; try { hashAlg = new SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(np.GetSaltPassword()); byte[] bytHash = hashAlg.ComputeHash(bytValue); np.SaltedHashedPassword = Convert.ToBase64String(bytHash); } catch (Exception e) { throw e; } finally { if (hashAlg != null) { hashAlg.Clear(); hashAlg.Dispose(); hashAlg = null; } } return np; }