/// <summary> /// /// </summary> /// <param name="Path"></param> /// <param name="Uppercase"></param> /// <param name="Error"></param> /// <returns></returns> public static string FILEtoSHA384(string Path, bool Uppercase = false, string Error = Constants.ErrorMessage) { try { if (File.Exists(Path)) { using SHA384 SHA384 = new SHA384CryptoServiceProvider(); using FileStream Stream = File.OpenRead(Path); byte[] Hash = SHA384.ComputeHash(Stream); return(Uppercase == false?BitConverter.ToString(Hash).Replace("-", "").ToLowerInvariant() : BitConverter.ToString(Hash).Replace("-", "").ToUpperInvariant()); } else { return(Error); } } catch { return(Error + Constants.ErrorTitle + "HH-FTS3!)"); } }
/// <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 { SHA384CryptoServiceProvider SHA384CSP = new SHA384CryptoServiceProvider(); byte[] bytValue = Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA384CSP.ComputeHash(bytValue); SHA384CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Length; 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); } }
private void login() { if (txtAccount.Text == "") { MessageBox.Show("請輸入帳號", "登入錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (txtPassword.Text == "") { MessageBox.Show("請輸入密碼", "登入錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { SHA384 sha384 = new SHA384CryptoServiceProvider(); //建立一個SHA384 byte[] source = Encoding.Default.GetBytes(txtPassword.Text); //將字串轉為Byte[] byte[] crypto = sha384.ComputeHash(source); //進行SHA384加密 string password = Convert.ToBase64String(crypto); //把加密後的字串從Byte[]轉為字串 int id = int.Parse(txtAccount.Text); if (db.checkLogin(id, password)) { if (checkBox1.Checked == true) { // 在 FormClosing 事件撰寫儲存設定程式碼 // 將帳號 TextBox.Text 儲存到 UsernameSetting MSPMaterial.Properties.Settings.Default.UserNameSetting = this.txtAccount.Text; MSPMaterial.Properties.Settings.Default.Save(); } this.Close(); thread = new Thread(openMainForm); thread.SetApartmentState(ApartmentState.STA); thread.Start(); } else { MessageBox.Show("帳號或密碼錯誤", "登入錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
} //compute hash from arguments and return hash value as string private static string GetSHA384Hash(string text) { //create variables for computing hash and making it a string UnicodeEncoding UniCode = new UnicodeEncoding(); byte[] HashResult; byte[] msg = UniCode.GetBytes(text); SHA384 hashString = new SHA384CryptoServiceProvider(); string Str = ""; //compute hash with SHA384 module and format output as string //convert bytes in HashResult to string values HashResult = hashString.ComputeHash(msg); foreach (byte x in HashResult) { Str += String.Format("{0:x2}", x); } //clear excess resource usage hashString.Clear(); return(Str); } //compute hash from arguments and return hash value as string
/// <summary> /// SHA384 加密 /// </summary> /// <param name="value">待加密字符串</param> /// <param name="encoding">编码格式</param> /// <returns></returns> public static string Sha384Encryption(string value, Encoding encoding = null) { if (string.IsNullOrEmpty(value)) { throw new ArgumentNullException("value", "待加密字符串不能为空"); } if (encoding == null) { encoding = Encoding.UTF8; } using (SHA384 sha384 = new SHA384CryptoServiceProvider()) { var strRes = Encoding.UTF8.GetBytes(value); strRes = sha384.ComputeHash(strRes); var sb = new StringBuilder(100); for (int i = 0; i < strRes.Length; i++) { sb.Append(strRes[i].ToString("x2")); } return(sb.ToString()); } }
public string SHA384_Encoding(string s) { try { String strData = ""; byte[] plainTextBytes = Ecd_.GetBytes(s); HashAlgorithm Hash; Hash = new SHA384CryptoServiceProvider(); byte[] hashed = Hash.ComputeHash(plainTextBytes); for (int i = 0; i < hashed.Length; ++i) { strData += string.Format("{0:x2}", hashed[i]); } //MD 5 변환 return(strData); } catch (Exception) { } return(null); }
private static string SHA384Crypt(string txt, Encoding encoding = null) { try { encoding = encoding ?? Encoding.UTF8; if (string.IsNullOrEmpty(txt)) { throw new Exception("原文不能为空!"); } SHA384CryptoServiceProvider sha = new SHA384CryptoServiceProvider(); byte[] org = encoding.GetBytes(txt); byte[] output = sha.ComputeHash(org); string retStr = ""; for (int i = 0; i < output.Length; i++) { retStr += output[i].ToString("x2"); } return(retStr); } catch (Exception) { throw; } }
private static string ComputeHash(string password) { SHA384CryptoServiceProvider hashAlgorithm = new SHA384CryptoServiceProvider(); return(Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(password)))); }
public ICustomActivityResult Execute() { StringWriter sw = new StringWriter(); DataTable dt = new DataTable("resultSet"); dt.Columns.Add("Result", typeof(string)); if (!File.Exists(SrcPath) || !File.Exists(DstPath)) { throw new Exception("File not found"); } using (FileStream SrcStream = new System.IO.FileStream(SrcPath, FileMode.Open, FileAccess.Read)) { using (FileStream DstStream = new System.IO.FileStream(DstPath, FileMode.Open, FileAccess.Read)) { byte[] Srcsum = null; byte[] Dstsum = null; switch (HashAlgorithm) { case "0": using (MD5 csp = new MD5CryptoServiceProvider()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; case "1": using (SHA1 csp = new SHA1CryptoServiceProvider()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; case "2": using (SHA256 csp = new SHA256CryptoServiceProvider()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; case "3": using (SHA384 csp = new SHA384CryptoServiceProvider()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; case "4": using (SHA512 csp = new SHA512CryptoServiceProvider()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; case "5": using (RIPEMD160 csp = new RIPEMD160Managed()) { Srcsum = csp.ComputeHash(SrcStream); Dstsum = csp.ComputeHash(DstStream); } break; default: throw new Exception("Invalid hashing algorithm."); } bool isEqual = true; if (Srcsum.Length != Dstsum.Length) { isEqual = false; } else { int i = 0; while (i < Srcsum.Length && isEqual == true) { if (!Srcsum[i].Equals(Dstsum[i])) { isEqual = false; } i++; } } if (isEqual) { dt.Rows.Add("True"); } else { dt.Rows.Add("False"); } } } return(this.GenerateActivityResult(dt)); }
private void Checksum(string algorithm, byte[] data) { if (algorithm == @"CRC32") { long crc = CRC32CryptoServiceProvider.Compute(data); OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", crc)); } else if (algorithm == @"CRC64") { ulong crc = CRC64CryptoServiceProvider.Compute(data); OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", crc)); } else if (algorithm == @"MD5") { using (MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } else if (algorithm == @"SHA1") { using (SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } else if (algorithm == @"SHA256") { using (SHA256CryptoServiceProvider hasher = new SHA256CryptoServiceProvider()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } else if (algorithm == @"SHA384") { using (SHA384CryptoServiceProvider hasher = new SHA384CryptoServiceProvider()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } else if (algorithm == @"SHA512") { using (SHA512CryptoServiceProvider hasher = new SHA512CryptoServiceProvider()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } else if (algorithm == @"RIPEMD160") { using (RIPEMD160 hasher = RIPEMD160Managed.Create()) { byte[] list = hasher.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Length; i++) { sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture)); } OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString())); } } }
/// <summary> /// Get the hashcode from the file. /// </summary> /// <param name="filename">The path and file name to generate the hash code for.</param> /// <param name="hashcodeType">The hash name.</param> /// <returns>The generated hash code.</returns> public static byte[] GetHashcodeFileRaw(string filename, Nequeo.Cryptography.HashcodeType hashcodeType) { FileStream file = null; byte[] hashValue = null; try { // Open the file to generate the hash code for. using (file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // Select the hash code type. switch (hashcodeType) { case HashcodeType.MD5: // MD5 hashcode. MD5 md5 = new MD5CryptoServiceProvider(); hashValue = md5.ComputeHash(file); break; case HashcodeType.SHA1: // SHA1 hashcode. SHA1 sha1 = new SHA1CryptoServiceProvider(); hashValue = sha1.ComputeHash(file); break; case HashcodeType.SHA256: // SHA256 hashcode. SHA256 sha256 = new SHA256CryptoServiceProvider(); hashValue = sha256.ComputeHash(file); break; case HashcodeType.SHA384: // SHA384 hashcode. SHA384 sha384 = new SHA384CryptoServiceProvider(); hashValue = sha384.ComputeHash(file); break; case HashcodeType.SHA512: // SHA512 hashcode. SHA512 sha512 = new SHA512CryptoServiceProvider(); hashValue = sha512.ComputeHash(file); break; } // Close the file. file.Close(); // Return the hash code. return(hashValue); } } catch (Exception) { throw; } finally { // Clean-up if (file != null) { file.Close(); } } }
//-----------------------------------------------------------------------------// // SHA384ハッシュ値生成(引数byte[]型) //-----------------------------------------------------------------------------// static public byte[] sha384_ComputeHash(byte[] srcArray) { SHA384CryptoServiceProvider sha384 = new SHA384CryptoServiceProvider(); return(sha384.ComputeHash(srcArray)); }
public byte[] ComputeHash(byte[] data) { var provider = new SHA384CryptoServiceProvider(); return(provider.ComputeHash(data)); }
public string SHA384(string str) { SHA384CryptoServiceProvider sha384 = new SHA384CryptoServiceProvider(); return(BitConverter.ToString((byte[])sha384.ComputeHash((byte[])Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty).ToLower()); }
//választás az algoritmusok között //választás a string és a fájl között public string CreateHash(HashType t, string szoveg, bool up = false) { string hash = ""; if (t == HashType.MD5) { byte[] data; MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider(); if (File.Exists(szoveg)) { data = File.ReadAllBytes(szoveg); } else { data = new UTF8Encoding().GetBytes(szoveg); } hash = ByteToHash(hasher.ComputeHash(data)); } else if (t == HashType.SHA1) { SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider(); byte[] data; if (File.Exists(szoveg)) { data = File.ReadAllBytes(szoveg); } else { data = new UTF8Encoding().GetBytes(szoveg); } hash = ByteToHash(hasher.ComputeHash(data)); } else if (t == HashType.SHA256) { SHA256CryptoServiceProvider hasher = new SHA256CryptoServiceProvider(); byte[] data; if (File.Exists(szoveg)) { data = File.ReadAllBytes(szoveg); } else { data = new UTF8Encoding().GetBytes(szoveg); } hash = ByteToHash(hasher.ComputeHash(data)); } else if (t == HashType.SHA384) { SHA384CryptoServiceProvider hasher = new SHA384CryptoServiceProvider(); byte[] data; if (File.Exists(szoveg)) { data = File.ReadAllBytes(szoveg); } else { data = new UTF8Encoding().GetBytes(szoveg); } hash = ByteToHash(hasher.ComputeHash(data)); } else { //SHA512 SHA512CryptoServiceProvider hasher = new SHA512CryptoServiceProvider(); byte[] data; if (File.Exists(szoveg)) { data = File.ReadAllBytes(szoveg); } else { data = new UTF8Encoding().GetBytes(szoveg); } hash = ByteToHash(hasher.ComputeHash(data)); } if (up) { return(hash.ToUpper()); } else { return(hash); } }
private byte[] ValidatePassword(byte[] paddedPassword, byte[] password, byte [] uValue) { byte[] key = new byte[Constants.KeySize]; byte[] iv = new byte[Constants.VectorSize]; byte[] E16 = new byte[16]; byte[] array = null; byte[] E = null; byte[] K1 = null; int idx = 0; //Take the SHA - 256 hash of the original input to the algorithm //and name the resulting 32 bytes, K. byte[] K = sha256.ComputeHash(paddedPassword, 0, paddedPassword.Length); //The conditional-OR operator (||) performs a logical - OR //of its bool operands. If the first operand evaluates to true, //the second operand isn't evaluated. If the first operand evaluates //to false, the second operator determines whether the OR expression //as a whole evaluates to true or false. while (idx < 64 || E[E.Length - 1] + 32 > idx) { Array.Copy(K, key, key.Length); Array.Copy(K, 16, iv, 0, iv.Length); //Make a new string, K1, consisting of 64 repetitions of the sequence: //input password, K, the 48 - byte user key. The 48 byte user key is //only used when checking the owner password or creating the owner key.If //checking the user password or creating the user key, K1 is the //concatenation of the input password and K. K1 = new byte[(password.Length + K.Length + uValue.Length) * 64]; array = arrayMath.ConcatByteArrays(password, K); array = arrayMath.ConcatByteArrays(array, uValue); for (int j = 0, pos = 0; j < 64; j++, pos += array.Length) { Array.Copy(array, 0, K1, pos, array.Length); } //Encrypt K1 with the AES-128(CBC, no padding) algorithm, //using the first 16 bytes of K as the key and the second 16 bytes of //K as the initialization vector. The result of this encryption is E. E = aes.CreateEncryptor(key, iv).TransformFinalBlock(K1, 0, K1.Length); //Now we have to take the first 16 bytes of an unsigned big endian integer... //and compute the remainder modulo 3. Taking the first 16 bytes of E //as an unsigned big-endian integer, compute the remainder, modulo 3. Array.Copy(E, E16, E16.Length); BigInteger bigInteger = new BigInteger(E16.Reverse().Concat(new byte[] { 0x00 }).ToArray()); byte[] result = BigInteger.Remainder(bigInteger, 3).ToByteArray(); switch (result[0]) { case 0x00: K = sha256.ComputeHash(E, 0, E.Length); break; case 0x01: K = sha384.ComputeHash(E, 0, E.Length); break; case 0x02: K = sha512.ComputeHash(E, 0, E.Length); break; default: throw new Exception("Unexpected result while computing the remainder, modulo 3."); } idx++; } return(K); }
/// <summary> /// Read the file and calculate the checksum ///</summary> /// <param name="type">the hash type to use</param> /// <param name="fileName">the file to read</param> /// <returns>the hex representation of the hash using uppercase chars</returns> public static String getFileHash(HashType type, String fileName) { try { var HashValue = new byte[0]; using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { switch (type) { case HashType.MD5: using (var h = new MD5CryptoServiceProvider()) { HashValue = h.ComputeHash(fileStream); } break; case HashType.SHA1: using (var h = new SHA1CryptoServiceProvider()) { HashValue = h.ComputeHash(fileStream); } break; case HashType.SHA256: using (var h = new SHA256CryptoServiceProvider()) { HashValue = h.ComputeHash(fileStream); } break; case HashType.SHA384: using (var h = new SHA384CryptoServiceProvider()) { HashValue = h.ComputeHash(fileStream); } break; case HashType.SHA512: using (var h = new SHA512CryptoServiceProvider()) { HashValue = h.ComputeHash(fileStream); } break; } } return(BitConverter.ToString(HashValue).Replace("-", String.Empty)); } catch (Exception ex) { return(ex.Message); } }
public static string SHA384(string str) { return(BitConverter.ToString((byte[])_sha384.ComputeHash((byte[])Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty).ToLower()); }
public ICustomActivityResult Execute() { StringWriter sw = new StringWriter(); DataTable dt = new DataTable("resultSet"); dt.Columns.Add("Result", typeof(string)); if (!File.Exists(Path)) { throw new Exception("File not found"); } using (FileStream reader = new System.IO.FileStream(Path, FileMode.Open, FileAccess.Read)) { byte[] hash = null; switch (HashAlgorithm) { case "0": using (MD5 csp = new MD5CryptoServiceProvider()) { hash = csp.ComputeHash(reader); } break; case "1": using (SHA1 csp = new SHA1CryptoServiceProvider()) { hash = csp.ComputeHash(reader); } break; case "2": using (SHA256 csp = new SHA256CryptoServiceProvider()) { hash = csp.ComputeHash(reader); } break; case "3": using (SHA384 csp = new SHA384CryptoServiceProvider()) { hash = csp.ComputeHash(reader); } break; case "4": using (SHA512 csp = new SHA512CryptoServiceProvider()) { hash = csp.ComputeHash(reader); } break; case "5": using (RIPEMD160 csp = new RIPEMD160Managed()) { hash = csp.ComputeHash(reader); } break; default: throw new Exception("Invalid hashing algorithm."); } dt.Rows.Add(ByteArrayToString(hash)); } return(this.GenerateActivityResult(dt)); }
internal static SumResult CalculateSums(string file) { SumResult result = new SumResult(); try { if (File.Exists(file)) { result.File = file; if (Options.CurrentOptions.HashOptions.MD5) { using (FileStream fs = File.OpenRead(file)) { using (var md5 = new MD5CryptoServiceProvider()) { result.MD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA1) { using (FileStream fs = File.OpenRead(file)) { using (var sha1 = new SHA1CryptoServiceProvider()) { result.SHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA256) { using (FileStream fs = File.OpenRead(file)) { using (var sha256 = new SHA256CryptoServiceProvider()) { result.SHA256 = BitConverter.ToString(sha256.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA384) { using (FileStream fs = File.OpenRead(file)) { using (var sha384 = new SHA384CryptoServiceProvider()) { result.SHA384 = BitConverter.ToString(sha384.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA512) { using (FileStream fs = File.OpenRead(file)) { using (var sha512 = new SHA512CryptoServiceProvider()) { result.SHA512 = BitConverter.ToString(sha512.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.CRC32) { using (FileStream fs = File.OpenRead(file)) { using (var crc32 = new Crc32Algorithm()) { result.CRC32 = BitConverter.ToString(crc32.ComputeHash(fs)).Replace("-", string.Empty); if (Options.CurrentOptions.CRC32Decimal) { result.CRC32 = Convert.ToInt64(result.CRC32, 16).ToString(); } } } } if (Options.CurrentOptions.HashOptions.RIPEMD160) { using (FileStream fs = File.OpenRead(file)) { using (var ripemd160 = RIPEMD160.Create()) { result.RIPEMD160 = BitConverter.ToString(ripemd160.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA3_256) { using (FileStream fs = File.OpenRead(file)) { using (var h = Sha3.Sha3256()) { result.SHA3_256 = BitConverter.ToString(h.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA3_384) { using (FileStream fs = File.OpenRead(file)) { using (var h = Sha3.Sha3384()) { result.SHA3_384 = BitConverter.ToString(h.ComputeHash(fs)).Replace("-", string.Empty); } } } if (Options.CurrentOptions.HashOptions.SHA3_512) { using (FileStream fs = File.OpenRead(file)) { using (var h = Sha3.Sha3512()) { result.SHA3_512 = BitConverter.ToString(h.ComputeHash(fs)).Replace("-", string.Empty); } } } } } catch (Exception ex) { result = null; MessageBox.Show(ex.Message, "Hash Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (Options.CurrentOptions.LowerCasing) { result.ConvertToLowerCasing(); } else { result.ConvertToUpperCasing(); } return(result); }
public static SHA384Checksum Generate(Stream input) { var SHA384 = new SHA384CryptoServiceProvider(); return(new SHA384Checksum(SHA384.ComputeHash(input))); }
/// <summary> /// SHA384 해쉬코드를 생성합니다. /// </summary> /// <param name="content">해쉬코드 생성에 사용할 문자열입니다.</param> /// <returns>생성된 SHA384 해쉬코드입니다.</returns> public static string SHA384(string content) { byte[] bytes = Encoding.UTF8.GetBytes(content); byte[] HashContent = SHA384Provider.ComputeHash(bytes); return(Convert.ToHexCode(HashContent)); }
private void HashingThreadForBigFiles() { try { FileStream GetFileToHash = new FileStream(textBox1.Text, FileMode.Open); switch (HashType) { case 1: SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider(); label3.Text = "Hashing...."; byte[] FileHashSHA512 = SHA512.ComputeHash(GetFileToHash); string TheFinalHashSHA512 = BitConverter.ToString(FileHashSHA512).Replace("-", string.Empty).ToLower(); textBox2.Text = TheFinalHashSHA512; GetFileToHash.Close(); label3.Text = "Done."; break; case 2: SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider(); label3.Text = "Hashing...."; byte[] FileHashSHA384 = SHA384.ComputeHash(GetFileToHash); string TheFinalHashSHA384 = BitConverter.ToString(FileHashSHA384).Replace("-", string.Empty).ToLower(); textBox2.Text = TheFinalHashSHA384; GetFileToHash.Close(); label3.Text = "Done."; break; case 3: SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider(); label3.Text = "Hashing...."; byte[] FileHashSHA256 = SHA256.ComputeHash(GetFileToHash); string TheFinalHashSHA256 = BitConverter.ToString(FileHashSHA256).Replace("-", string.Empty).ToLower(); textBox2.Text = TheFinalHashSHA256; GetFileToHash.Close(); label3.Text = "Done."; break; case 4: SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider(); label3.Text = "Hashing...."; byte[] FileHashSHA1 = SHA1.ComputeHash(GetFileToHash); string TheFinalHashSHA1 = BitConverter.ToString(FileHashSHA1).Replace("-", string.Empty).ToLower(); textBox2.Text = TheFinalHashSHA1; GetFileToHash.Close(); label3.Text = "Done."; break; case 5: MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); label3.Text = "Hashing...."; byte[] FileHashMD5 = MD5.ComputeHash(GetFileToHash); string TheFinalHashMD5 = BitConverter.ToString(FileHashMD5).Replace("-", string.Empty).ToLower(); textBox2.Text = TheFinalHashMD5; GetFileToHash.Close(); label3.Text = "Done."; break; default: MessageBox.Show("Please Select a hash mode to hash the File.", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); break; } GetFileToHash.Close(); } catch (Exception ex) { label3.Text = "Error"; MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } }
/// <summary> /// Get the hashcode from the file. /// </summary> /// <param name="filename">The path and file name to generate the hash code for.</param> /// <param name="hashcodeType">The hash name.</param> /// <returns>The generated hash code.</returns> public static string GetHashcodeFile(string filename, Nequeo.Cryptography.HashcodeType hashcodeType) { FileStream file = null; byte[] hashValue = null; StringBuilder sb = null; try { // Open the file to generate the hash code for. using (file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // Select the hash code type. switch (hashcodeType) { case HashcodeType.MD5: // MD5 hashcode. MD5 md5 = new MD5CryptoServiceProvider(); hashValue = md5.ComputeHash(file); break; case HashcodeType.SHA1: // SHA1 hashcode. SHA1 sha1 = new SHA1CryptoServiceProvider(); hashValue = sha1.ComputeHash(file); break; case HashcodeType.SHA256: // SHA256 hashcode. SHA256 sha256 = new SHA256CryptoServiceProvider(); hashValue = sha256.ComputeHash(file); break; case HashcodeType.SHA384: // SHA384 hashcode. SHA384 sha384 = new SHA384CryptoServiceProvider(); hashValue = sha384.ComputeHash(file); break; case HashcodeType.SHA512: // SHA512 hashcode. SHA512 sha512 = new SHA512CryptoServiceProvider(); hashValue = sha512.ComputeHash(file); break; case HashcodeType.SHA3: // SHA3 hashcode. Sha3.SHA3Managed sha3 = new Sha3.SHA3Managed(); hashValue = sha3.ComputeHash(file); break; case HashcodeType.PBKDF2: throw new Exception("PBKDF2 Hashing is not supported."); } // Close the file. file.Close(); // Get the hashcode bytes as hex-string. sb = new StringBuilder(); for (int i = 0; i < hashValue.Length; i++) { sb.Append(hashValue[i].ToString("X2")); } // Return the hash code. return(sb.ToString()); } } catch (Exception) { throw; } finally { // Clean-up if (file != null) { file.Close(); } } }
public byte[] Calculate(byte[] input) { return(Algorithm.ComputeHash(input)); }
private static byte[] GetHash(string password) { SHA384 sha = new SHA384CryptoServiceProvider(); return(sha.ComputeHash(Encoding.UTF8.GetBytes(password))); }
public string Encrypt(EncoderType type, string encrypt) { string ret = ""; byte[] inputByteArray = Encoding.UTF8.GetBytes(encrypt); byte[] rgbKey = Convert.FromBase64String(Key); byte[] rgbIV = Convert.FromBase64String(IV); switch (type) { case EncoderType.AES: using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider()) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); ret = Convert.ToBase64String(ms.ToArray()); } } } break; case EncoderType.DES: using (DESCryptoServiceProvider csp = new DESCryptoServiceProvider()) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); ret = Convert.ToBase64String(ms.ToArray()); } } } break; case EncoderType.RC2: using (RC2CryptoServiceProvider csp = new RC2CryptoServiceProvider()) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); ret = Convert.ToBase64String(ms.ToArray()); } } } break; case EncoderType.TripleDES: using (TripleDESCryptoServiceProvider csp = new TripleDESCryptoServiceProvider()) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); ret = Convert.ToBase64String(ms.ToArray()); } } } break; case EncoderType.RSA: using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider()) { csp.FromXmlString(Key); ret = Convert.ToBase64String(csp.Encrypt(inputByteArray, false)); } break; case EncoderType.MD5: using (MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider()) { ret = BitConverter.ToString(csp.ComputeHash(inputByteArray)).Replace("-", string.Empty); } break; case EncoderType.SHA1: using (SHA1CryptoServiceProvider csp = new SHA1CryptoServiceProvider()) { ret = BitConverter.ToString(csp.ComputeHash(inputByteArray)).Replace("-", string.Empty); } break; case EncoderType.SHA256: using (SHA256CryptoServiceProvider csp = new SHA256CryptoServiceProvider()) { ret = BitConverter.ToString(csp.ComputeHash(inputByteArray)).Replace("-", string.Empty); } break; case EncoderType.SHA384: using (SHA384CryptoServiceProvider csp = new SHA384CryptoServiceProvider()) { ret = BitConverter.ToString(csp.ComputeHash(inputByteArray)).Replace("-", string.Empty); } break; case EncoderType.SHA512: using (SHA512CryptoServiceProvider csp = new SHA512CryptoServiceProvider()) { ret = BitConverter.ToString(csp.ComputeHash(inputByteArray)).Replace("-", string.Empty); } break; } return(ret); }
public static string CalculateSHA384(File file) { using (var service = new SHA384CryptoServiceProvider()) return(CalculateOutput(service.ComputeHash(file.GetBytes()))); }
/// <summary> /// Performas a sha384 hash on the input string /// </summary> /// <param name="input">String to perform the hash on</param> /// <returns>SHA384 hash output</returns> static public byte[] hashFunction(string input) { var sha384 = new SHA384CryptoServiceProvider(); return(sha384.ComputeHash(Encoding.UTF8.GetBytes(input))); }