public static bool LogFileEquivalence(bool expectMatch, string file1, string file2) { System.Security.Cryptography.SHA1 hasher = System.Security.Cryptography.SHA1.Create(); byte[] file1hash = null; byte[] file2hash = null; using (Stream file1strm = File.OpenRead(file1)) { using (Stream file2strm = File.OpenRead(file2)) { file1hash = hasher.ComputeHash(file1strm); file2hash = hasher.ComputeHash(file2strm); } } if (null == file1hash || null == file2hash) { LogFail("The two files did not hash correctly."); return(false); } if (file1hash.Length != file2hash.Length) { LogFail("The two files did not hash correctly."); return(false); } bool allMatched = true; for (int i = 0; i < file1hash.Length; ++i) { if (expectMatch && file1hash[i] != file2hash[i]) { LogFail(Path.GetFileName(file1) + " != " + Path.GetFileName(file2)); return(false); } else if (!expectMatch && file1hash[1] != file2hash[i]) { allMatched = false; } } if (!expectMatch && allMatched) { LogFail(Path.GetFileName(file1) + " == " + Path.GetFileName(file2)); } else if (expectMatch) { LogPass(Path.GetFileName(file1) + " == " + Path.GetFileName(file2)); } else { LogPass(Path.GetFileName(file1) + " != " + Path.GetFileName(file2)); } return(true); }
public static string ComputeSHA1(string path) { if (!File.Exists(path)) { throw new NotFileException(path, nameof(path)); } string hashSHA1 = string.Empty; //检查文件是否存在,如果文件存在则进行计算,否则返回空值 using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { //计算文件的SHA1值 System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create(); Byte[] buffer = calculator.ComputeHash(fs); calculator.Clear(); //将字节数组转换成十六进制的字符串形式 StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < buffer.Length; i++) { stringBuilder.Append(buffer[i].ToString("x2")); } hashSHA1 = stringBuilder.ToString(); }//关闭文件流 return(hashSHA1); }
public void GetWsPasswordDigest(string user, string password, string nonce, string timestamp, string digest = "") { var nonceDecodeBinary = Convert.FromBase64String(nonce); byte[] dateBinary = Encoding.UTF8.GetBytes(timestamp); byte[] passwordBinary = Encoding.UTF8.GetBytes(password); Console.WriteLine(string.Format("Nonce decoded from B64 -> Hex: {0} ", BitConverter.ToString(nonceDecodeBinary))); byte[] concatData = new byte[nonceDecodeBinary.Length + dateBinary.Length + passwordBinary.Length]; Buffer.BlockCopy(nonceDecodeBinary, 0, concatData, 0, nonceDecodeBinary.Length); Buffer.BlockCopy(dateBinary, 0, concatData, nonceDecodeBinary.Length, dateBinary.Length); Buffer.BlockCopy(passwordBinary, 0, concatData, nonceDecodeBinary.Length + dateBinary.Length, passwordBinary.Length); System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); string computedDigest = Convert.ToBase64String(sha1.ComputeHash(concatData)); Console.WriteLine(string.Format("Current Hash:\t{0}\nOriginal Hash:\t{1}", computedDigest, digest)); if (digest != "") { if (computedDigest == digest) { MessageBox.Show("Hash match" + txtPassword.Text); } else { MessageBox.Show(string.Format("Hash mismatch\nActual\t{0}\nCalc\t{1}", digest, computedDigest)); } } }
private static byte[] GetHashCode(string compiledFile, string[] scriptFiles, bool debug) { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bin = new BinaryWriter(ms)) { FileInfo fileInfo = new FileInfo(compiledFile); bin.Write(fileInfo.LastWriteTimeUtc.Ticks); foreach (string scriptFile in scriptFiles) { fileInfo = new FileInfo(scriptFile); bin.Write(fileInfo.LastWriteTimeUtc.Ticks); } bin.Write(debug); ms.Position = 0; using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) { return(sha1.ComputeHash(ms)); } } } }
public static string Sha1(string fileName) { string returnValue = null; using (System.IO.FileStream fs = System.IO.File.OpenRead(fileName)) { using (System.IO.BufferedStream bs = new System.IO.BufferedStream(fs)) { using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) { byte[] hash = sha1.ComputeHash(bs); System.Text.StringBuilder formatted = new System.Text.StringBuilder(2 * hash.Length); for (int i = 0; i < hash.Length; ++i) { formatted.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0:x2}", hash[i]); } // Next b returnValue = formatted.ToString(); formatted.Length = 0; formatted = null; } // End Sha1 } // End Using bs } // End Using fs return(returnValue); } // End Function Sha1
public static byte[] ComputeSHA1HashByte(this string input) { using (System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create()) { return(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input))); } }
/// <summary> /// Get a Hash code of the running app /// </summary> public static string GetAppHashCode() { string filename = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; string hashstring = string.Empty; if (File.Exists(filename)) { try { using (System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create()) { byte[] hash; using (FileStream stream = File.OpenRead(filename)) { hash = sha.ComputeHash(stream); } foreach (byte item in hash) { hashstring += item.ToString("x2"); } } } catch { } } return(hashstring); }
/// <summary> /// 添加一个用户到当前列表,表示该用户已登录 /// </summary> /// <param name="user">用户名</param> /// <param name="password">密码</param> /// <param name="sid">唯一ID</param> /// <param name="e">地址</param> /// <returns></returns> private static bool AddUserInList(string user, string password, ref string sid, IPEndPoint e) { try { System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); sid = Convert.ToBase64String(hash.ComputeHash(SendMessage.ToBytes(user + password + e + DateTime.Now))); UserInfo info = new UserInfo() { user = user, password = password, e = e.ToString(), lastTime = DateTime.Now, sid = sid }; if (now.Contains(user)) { now[user] = info; } else { lock (now) { now.Add(user, info); } } } catch (Exception ex) { Log.a("Users can not be increased:" + ex.Message); return(false); } return(true); }
public bool HashPassword() { bool result = true; string _password_hash = string.Empty; try { if (!string.IsNullOrEmpty(_password_salt) && _password_salt.Contains("!PASSWORD!")) { System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); string str = _password_salt.Replace("!PASSWORD!", _password); _password_hash = BitConverter.ToString(hash.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLower(); } else { result = false; } } catch { result = false; } //We have password hash now, replace password field with the hash. if (result) { Password = _password_hash; } return(result); }
private void button3_Click(object sender, EventArgs e) { string url = textBox1.Text; GameItem item = listBox2.SelectedItem as GameItem; if (item == null) { MessageBox.Show("选中一个Game"); return; } try { byte[] sb = System.Text.Encoding.UTF8.GetBytes(textBox4.Text); byte[] wt = sha1.ComputeHash(sb); string code = Convert.ToBase64String(sb, 0, sb.Length); byte[] br = wc.DownloadData(url + "?c=login&g=" + item.name + "&u=" + textBox3.Text + "&p=" + code); string returnstr = System.Text.Encoding.UTF8.GetString(br); listBox1.Items.Add(new Log("login result", returnstr)); var json = MyJson.Parse(returnstr); if (json.GetDictItem("status").AsInt() == 0) { mytoken = json.GetDictItem("token").AsString(); this.Text = "mytoken:" + mytoken; textBox6.Text = mytoken; } } catch (Exception err) { listBox1.Items.Add(new Log("http err", err.ToString())); } }
/// <summary> /// 计算指定文件的SHA1值 /// </summary> /// <param name="fileName">指定文件的完全限定名称</param> /// <returns>返回值的字符串形式</returns> public static String ComputeSHA1(String fileName) { String hashSHA1 = String.Empty; try { //检查文件是否存在,如果文件存在则进行计算,否则返回空值 if (System.IO.File.Exists(fileName)) { using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { //计算文件的SHA1值 System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create(); Byte[] buffer = calculator.ComputeHash(fs); calculator.Clear(); //将字节数组转换成十六进制的字符串形式 StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < buffer.Length; i++) { stringBuilder.Append(buffer[i].ToString("x2")); } hashSHA1 = stringBuilder.ToString(); }//关闭文件流 } } catch (Exception ex) { } return(hashSHA1); }
public string Encrypt(string str) { System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create(); byte[] preHash = System.Text.Encoding.UTF32.GetBytes(str); byte[] hash = sha.ComputeHash(preHash); return(System.Convert.ToBase64String(hash)); }
private bool HashCheck(byte[] newFile, byte[] tmdHash) { System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create(); byte[] fileHash = sha.ComputeHash(newFile); return(Wii.Tools.CompareByteArrays(fileHash, tmdHash)); }
/// <summary> /// Hash SHA-1 de um Conteúdo em Array de Bytes /// </summary> /// <param name="input"></param> /// <returns>Retorna o Hash em string Hexadecimal</returns> public static string SHA1(byte[] input) { using System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); return(BitConverter.ToString(hash.ComputeHash(input)) .Replace("-", "").ToLower()); }
} //ComputeCRC32 /// <summary> /// 获取文件的SHA1 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static String GetSHA1(String fileName) { String hashSHA1 = String.Empty; //检查文件是否存在,如果文件存在则进行计算,否则返回空值 if (File.Exists(fileName)) { using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { //计算文件的SHA1值 System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create(); Byte[] buffer = calculator.ComputeHash(fileStream); calculator.Clear(); //将字节数组转换成十六进制的字符串形式 StringBuilder stringBuilder = new StringBuilder(); for (int bufferIdx = 0; bufferIdx < buffer.Length; bufferIdx++) { stringBuilder.Append(buffer[bufferIdx].ToString("x2")); } hashSHA1 = stringBuilder.ToString(); } //关闭文件流 } else { Console.Error.WriteLine("{0}文件找不到!", fileName); } return(hashSHA1); } //end GetSHA1
/// <summary> /// 获取字符串的SHA1 /// </summary> /// <param name="_this"></param> /// <param name="_UpCase">是否大写</param> /// <returns></returns> public static String SHA1(this String _this, Boolean _UpCase = false) { System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); Byte[] bytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(_this)); String hash = BitConverter.ToString(bytes).Replace("-", ""); return(_UpCase?hash:hash.ToLower()); }
/// <summary> /// 对敏感数据进行哈希加密 /// </summary> /// <param name="code">需要加密的字段</param> /// <returns></returns> public static string Hash(string code, long memberID) { tb_Mem_Member member = GetMemberByID(memberID); System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1Managed.Create(); code = Convert.ToBase64String(hash.ComputeHash(System.Text.Encoding.Unicode.GetBytes(code + member.Salt))); return(code); }
public static string SHA1(string value) { byte[] inBuf = Encoding.UTF8.GetBytes(value); System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); var hash = sha1.ComputeHash(inBuf); return(OtpTools.ByteArrayToHexString(hash)); }
public byte[] HashPassword(string password) { System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); byte[] combined = encoder.GetBytes(password); hash.ComputeHash(combined); return(hash.Hash); }
public static string GenerateHashString(string s) { System.Security.Cryptography.SHA1 sha1enc = System.Security.Cryptography.SHA1.Create(); byte[] wordbytes = Encoding.UTF8.GetBytes(s); sha1enc.ComputeHash(wordbytes); string hash = BitConverter.ToString(sha1enc.Hash).Replace("-", ""); return(hash); }
public static (byte[] hash, byte[] file) MakeRandomFile(int size, Random?random = null) { byte[] data = new byte[size]; if (random == null) { random = new Random(); } random.NextBytes(data); return(Hasher.ComputeHash(data), data); }
public static string SHA1Encrypt(string source) { byte[] sourceBytes = Encoding.UTF8.GetBytes(source); using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) { var targetBytes = sha1.ComputeHash(sourceBytes); return(string.Join("", targetBytes.Select(i => i.ToString("x2")))); } }
public byte[] GetHash(int piece) { byte[] data = ReadPiece(piece); if (data == null) { return(null); } return(sha1.ComputeHash(data)); }
public byte[] GetHash(byte[] data) { byte[] hash = null; using (System.Security.Cryptography.SHA1 crypto = System.Security.Cryptography.SHA1.Create()) { hash = crypto.ComputeHash(data); } return(hash); }
string ICedts_PartnerRepository.HashPassword(string str) { string rethash = string.Empty; System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); byte[] combined = encoder.GetBytes(str); hash.ComputeHash(combined); rethash = Convert.ToBase64String(hash.Hash); return(rethash); }
public string GetHash(string filename) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); byte[] bs = sha1.ComputeHash(fs); sha1.Clear(); string h = BitConverter.ToString(bs).ToLower().Replace("-", ""); return($"{h}_{Model}_{PitchShift:0.0000}_{FormantShift}"); } }
private string HashPassword(string str) { string rethash = ""; System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); byte[] combined = encoder.GetBytes(str); hash.ComputeHash(combined); rethash = Convert.ToBase64String(hash.Hash); return(rethash); }
public static string Sha1Hex(string value) { System.Security.Cryptography.SHA1 algorithm = System.Security.Cryptography.SHA1.Create(); byte[] data = algorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)); string sh1 = ""; for (int i = 0; i < data.Length; i++) { sh1 += data[i].ToString("x2").ToUpperInvariant(); } return(sh1); }
internal static byte[] CalculateHash(string plainText) { if (String.IsNullOrEmpty(plainText)) { throw new ArgumentNullException(nameof(plainText)); } System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); byte[] hashedBytes = sha1.ComputeHash(plainTextBytes); return(hashedBytes); }
}//ComputeCRC32 /// <summary> /// 计算指定文件的SHA1值 /// </summary> /// <param name="fs">文件流</param> /// <returns>返回值的字符串形式</returns> public static Byte[] SHA1(FileStream fs) { String hashSHA1 = String.Empty; //检查文件是否存在,如果文件存在则进行计算,否则返回空值 //计算文件的SHA1值 System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create(); Byte[] buffer = calculator.ComputeHash(fs); calculator.Clear(); return(buffer); }