private static string GetSha384Hash(string text) { using (var sha = new System.Security.Cryptography.SHA384Managed()) { return(Convert.ToBase64String(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text)))); } }
/// <summary> /// Make hashed password with Salt and hard coded block for improving security /// </summary> /// <param name="shelter"></param> /// <param name="Salted"></param> /// <returns></returns> public static string GetHash(System.Security.SecureString inputpassword, string Salted) { IntPtr unsafePtr = IntPtr.Zero; byte[] MergedArray = null; try { unsafePtr = System.Runtime.InteropServices.Marshal.SecureStringToGlobalAllocAnsi(inputpassword); byte[] myByteArray = new byte[inputpassword.Length]; System.Runtime.InteropServices.Marshal.Copy(unsafePtr, myByteArray, 0, inputpassword.Length); byte[] saltbuf = System.Text.Encoding.ASCII.GetBytes(Salted); byte[] hardcode = System.Text.Encoding.ASCII.GetBytes("U-m63W4HP2QCOz+u7UL9awx01Zg="); MergedArray = new byte[saltbuf.Length + myByteArray.Length + hardcode.Length]; System.Buffer.BlockCopy(saltbuf, 0, MergedArray, 0, saltbuf.Length); System.Buffer.BlockCopy(myByteArray, 0, MergedArray, saltbuf.Length, myByteArray.Length); System.Buffer.BlockCopy(hardcode, 0, MergedArray, saltbuf.Length + myByteArray.Length, hardcode.Length); MergedArray = new System.Security.Cryptography.SHA384Managed().ComputeHash(MergedArray); } catch (Exception ex) { throw ex; } finally { System.Runtime.InteropServices.Marshal.ZeroFreeGlobalAllocAnsi(unsafePtr); inputpassword.Dispose(); } return(Convert.ToBase64String(MergedArray)); }
public string GetSHA384Hash(string pathName) { string strResult = ""; string strHashData = ""; byte[] arrbytHashValue; System.IO.FileStream oFileStream = null; System.Security.Cryptography.SHA384 oSHA384Hasher = new System.Security.Cryptography.SHA384Managed(); try { oFileStream = GetFileStream(pathName); arrbytHashValue = oSHA384Hasher.ComputeHash(oFileStream); oFileStream.Close(); strHashData = System.BitConverter.ToString(arrbytHashValue); strHashData = strHashData.Replace("-", ""); strResult = strHashData; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1); } return(strResult); }
/// <summary> /// Generates hash equivalent for the plain text data /// </summary> /// <param name="inputText">Plain text input string</param> /// <returns></returns> public string ComputeHash(string inputText) { if (string.IsNullOrWhiteSpace(inputText)) { throw new NullReferenceException("inputText should not be empty!"); } byte[] _saltBytes; if (string.IsNullOrWhiteSpace(saltValue)) { saltValue = System.Web.Security.Membership.GeneratePassword(128, 64); } _saltBytes = Encoding.UTF8.GetBytes(saltValue); byte[] _plainTextBytes = Encoding.UTF8.GetBytes(inputText); byte[] _plainTextWithSaltBytes = new byte[_plainTextBytes.Length + _saltBytes.Length]; _plainTextBytes.CopyTo(_plainTextWithSaltBytes, 0); _saltBytes.CopyTo(_plainTextWithSaltBytes, _plainTextBytes.Length); System.Security.Cryptography.HashAlgorithm _hash; switch (_hashAlgorithm) { case CustomHashAlgorithm.HashAlgorithm.SHA1: _hash = new System.Security.Cryptography.SHA1Managed(); break; case CustomHashAlgorithm.HashAlgorithm.SHA256: _hash = new System.Security.Cryptography.SHA256Managed(); break; case CustomHashAlgorithm.HashAlgorithm.SHA384: _hash = new System.Security.Cryptography.SHA384Managed(); break; default: _hash = new System.Security.Cryptography.SHA512Managed(); break; } return Convert.ToBase64String(_hash.ComputeHash(_plainTextWithSaltBytes)); }
/// <summary> /// SHA384加密,不可逆转 /// </summary> /// <param name="str">string str:被加密的字符串</param> /// <returns>返回加密后的字符串</returns> private string SHA384Encrypt(string str) { System.Security.Cryptography.SHA384 s384 = new System.Security.Cryptography.SHA384Managed(); byte[] byte1; byte1 = s384.ComputeHash(Encoding.Default.GetBytes(str)); s384.Clear(); return Convert.ToBase64String(byte1); }
public string GetSHA384Hash(string pathName) { string strResult = ""; string strHashData = ""; byte[] arrbytHashValue; System.IO.FileStream oFileStream = null; System.Security.Cryptography.SHA384 oSHA384Hasher = new System.Security.Cryptography.SHA384Managed(); try { oFileStream = GetFileStream(pathName); arrbytHashValue = oSHA384Hasher.ComputeHash(oFileStream); oFileStream.Close(); strHashData = System.BitConverter.ToString(arrbytHashValue); strHashData = strHashData.Replace("-", ""); strResult = strHashData; } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1); } return (strResult); }