/// <summary> /// SHA-384 /// </summary> /// <param name="str"></param> public static string SHA_384(string str) { var sha384Csp = new System.Security.Cryptography.SHA384CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str); byte[] bytHash = sha384Csp.ComputeHash(bytValue); sha384Csp.Clear(); string hashStr = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; var temptr = ""; if (i > 9) { temptr = ((char)(i - 10 + 0x41)).ToString(); } else { temptr = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { temptr += ((char)(i - 10 + 0x41)).ToString(); } else { temptr += ((char)(i + 0x30)).ToString(); } hashStr += temptr; } return(hashStr); }
private static string GenerateFileHash(string hashType, string pathToFile) { try { using (var stream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read, FileShare.None)) { if (hashType == "md5") { using (var hash = new System.Security.Cryptography.MD5CryptoServiceProvider()) { var data = hash.ComputeHash(stream); return(BitConverter.ToString(data).Replace("-", "").ToLower()); } } else if (hashType == "sha1") { using (var hash = new System.Security.Cryptography.SHA1CryptoServiceProvider()) { var data = hash.ComputeHash(stream); return(BitConverter.ToString(data).Replace("-", "").ToLower()); } } else if (hashType == "sha256") { using (var hash = new System.Security.Cryptography.SHA256CryptoServiceProvider()) { var data = hash.ComputeHash(stream); return(BitConverter.ToString(data).Replace("-", "").ToLower()); } } else if (hashType == "sha384") { using (var hash = new System.Security.Cryptography.SHA384CryptoServiceProvider()) { var data = hash.ComputeHash(stream); return(BitConverter.ToString(data).Replace("-", "").ToLower()); } } else if (hashType == "sha512") { using (var hash = new System.Security.Cryptography.SHA512CryptoServiceProvider()) { var data = hash.ComputeHash(stream); return(BitConverter.ToString(data).Replace("-", "").ToLower()); } } else { throw new Exception("Unsupported hashType: " + hashType); } } } catch (Exception e) { return("Error: " + e.Message); } }
public static string Encrypt(string text) { if (string.IsNullOrWhiteSpace(text)) { return(""); } byte[] byteArray = Encoding.UTF8.GetBytes(text); using (System.Security.Cryptography.SHA384CryptoServiceProvider shaCSP = new System.Security.Cryptography.SHA384CryptoServiceProvider()) { return(BitConverter.ToString(shaCSP.ComputeHash(byteArray)).Replace("-", "")); } }
/// <summary> /// It is used to calculate the SHA384 Hash data of the file in the specified location. /// </summary> /// <param name="file">The location of the file in string value.</param> /// <returns>SHA384 output of the file will be returned.</returns> public string FileFingerprint(string file) { System.Security.Cryptography.SHA384 ee = new System.Security.Cryptography.SHA384CryptoServiceProvider(); ee.ComputeHash(File.ReadAllBytes(file)); byte[] result = ee.Hash; StringBuilder sb = new StringBuilder(); for (int i = 0; i < result.Length; i++) { sb.Append(result[i].ToString("x2")); } return(sb.ToString()); }
/// <summary> /// It returns the entered data as SHA384 Hash. /// </summary> /// <param name="content">Text in string value to be converted to SHA384.</param> /// <returns>SHA384 output will be returned.</returns> public string Create(string content) { System.Security.Cryptography.SHA384 ee = new System.Security.Cryptography.SHA384CryptoServiceProvider(); ee.ComputeHash(UTF8Encoding.UTF8.GetBytes(content)); byte[] result = ee.Hash; StringBuilder sb = new StringBuilder(); for (int i = 0; i < result.Length; i++) { sb.Append(result[i].ToString("x2")); } return(sb.ToString()); }
public static string EncryptFile(string path) { try { byte[] ba; using (var csp = new System.Security.Cryptography.SHA384CryptoServiceProvider()) { using (FileStream fs = File.OpenRead(path)) ba = csp.ComputeHash(fs); } return(Convert.ByteArrayToString(ba)); } catch (Exception ex) { Log.Debug(ex); return(string.Empty); } }
/// <summary> /// 计算SHA-384码 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string Hash_SHA_384(string word, bool toUpper = true) { try { System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA384CSP.ComputeHash(bytValue); SHA384CSP.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> /// Hash 值检查 /// </summary> /// <param name="resFile">文件路径</param> /// <param name="type">Hash 值的类型</param> public CheckHash(string resFile, HashType type) { hashValue = string.Empty; byte[] retVal = null; FileStream file = new FileStream(resFile, FileMode.Open); switch (type) { case HashType.MD5: System.Security.Cryptography.MD5CryptoServiceProvider MD5Hash = new System.Security.Cryptography.MD5CryptoServiceProvider(); retVal = MD5Hash.ComputeHash(file); break; case HashType.SHA1: System.Security.Cryptography.SHA1CryptoServiceProvider SHA1Hash = new System.Security.Cryptography.SHA1CryptoServiceProvider(); retVal = SHA1Hash.ComputeHash(file); break; case HashType.SHA256: System.Security.Cryptography.SHA256CryptoServiceProvider SHA256Hash = new System.Security.Cryptography.SHA256CryptoServiceProvider(); retVal = SHA256Hash.ComputeHash(file); break; case HashType.SHA384: System.Security.Cryptography.SHA384CryptoServiceProvider SHA384Hash = new System.Security.Cryptography.SHA384CryptoServiceProvider(); retVal = SHA384Hash.ComputeHash(file); break; case HashType.SHA512: System.Security.Cryptography.SHA512CryptoServiceProvider SHA512Hash = new System.Security.Cryptography.SHA512CryptoServiceProvider(); retVal = SHA512Hash.ComputeHash(file); break; } file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } hashValue = sb.ToString(); }
static string GetFileHash(string filePath, string hashType) { byte[] bs = null; switch (hashType) { case "SHA1": var sHA1 = new System.Security.Cryptography.SHA1CryptoServiceProvider(); bs = sHA1.ComputeHash(new FileStream(filePath, FileMode.Open)); break; case "SHA256": var sHA256 = new System.Security.Cryptography.SHA256CryptoServiceProvider(); bs = sHA256.ComputeHash(new FileStream(filePath, FileMode.Open)); break; case "SHA384": var sHA384 = new System.Security.Cryptography.SHA384CryptoServiceProvider(); bs = sHA384.ComputeHash(new FileStream(filePath, FileMode.Open)); break; case "SHA512": var sHA512 = new System.Security.Cryptography.SHA512CryptoServiceProvider(); bs = sHA512.ComputeHash(new FileStream(filePath, FileMode.Open)); break; case "MD5": var mD5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); bs = mD5.ComputeHash(new FileStream(filePath, FileMode.Open)); break; } string hashString = ""; if (bs == null) { return(null); } for (int i = 0; i < bs.Length; i++) { hashString += bs[i].ToString("x2");//"x2":两位十六进制 } return(hashString); }
private void button_Hash_Click(object sender, EventArgs e) { string strResult = ""; string strHashData = ""; byte[] arrbytHashValue; System.IO.FileStream oFileStream = null; if (comboBox1.Text == "") { const string message = "Please Choose a Hash Type"; const string caption = "Hash Type"; var result = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (label_Path.Text == "") { const string message = "Please browes for a file.."; const string caption = "Open File"; var result = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { if (comboBox1.SelectedIndex == 0) { System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); try { oFileStream = GetFileStream(filePath); arrbytHashValue = oMD5Hasher.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); } textBox1.Text = strResult; } else if (comboBox1.SelectedIndex == 1) { System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher = new System.Security.Cryptography.SHA1CryptoServiceProvider(); try { oFileStream = GetFileStream(filePath); arrbytHashValue = oSHA1Hasher.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); } textBox1.Text = strResult; } else if (comboBox1.SelectedIndex == 2) { System.Security.Cryptography.SHA256CryptoServiceProvider oSHA256Hasher = new System.Security.Cryptography.SHA256CryptoServiceProvider(); try { oFileStream = GetFileStream(filePath); arrbytHashValue = oSHA256Hasher.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); } textBox1.Text = strResult; } else if (comboBox1.SelectedIndex == 3) { System.Security.Cryptography.SHA384CryptoServiceProvider oSHA384Hasher = new System.Security.Cryptography.SHA384CryptoServiceProvider(); try { oFileStream = GetFileStream(filePath); 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); } textBox1.Text = strResult; } else if (comboBox1.SelectedIndex == 4) { System.Security.Cryptography.SHA512CryptoServiceProvider oSHA512Hasher = new System.Security.Cryptography.SHA512CryptoServiceProvider(); try { oFileStream = GetFileStream(filePath); arrbytHashValue = oSHA512Hasher.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); } textBox1.Text = strResult; } } }
/// <summary> /// SHA384 hash algorithm. /// </summary> public SHA384() { csp = new System.Security.Cryptography.SHA384CryptoServiceProvider(); }
/// <summary> /// 计算SHA-384码 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param> /// <returns></returns> public static string Hash_SHA_384(string word, bool toUpper = true) { try { System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA384CSP.ComputeHash(bytValue); SHA384CSP.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); } }