public static string Git_Blob_Sha1_Hash(byte[] buf) { // Encoding.ASCII is 7bit encoding but we want 8bit, so we need to use "iso-8859-1" var enc = Encoding.GetEncoding("iso-8859-1"); var head = String.Format("blob {0}\0{1}", buf.Length, enc.GetString(buf)); /* * StringBuilder dat = new StringBuilder(); * dat.Append(head); * dat.Append(Encoding.ASCII.GetString(buf)); * //PLog.Info("HEAD: size({0}) content: '{1}'", dat.Length, dat.ToString()); * byte[] blob_buf = Encoding.ASCII.GetBytes(dat.ToString()); */ var sha = SHA1.Create(); var hash = sha.ComputeHash(enc.GetBytes(head)); var sb = new StringBuilder(); foreach (var HashByte in hash) { sb.Append(HashByte.ToString("x2")); } const string hash_foobar = "323fae03f4606ea9991df8befbb2fca795e648fa"; // Correct GIT hash for a file containing only "foobar\n" var match = (String.CompareOrdinal(sb.ToString(), hash_foobar) == 0); //PLog.Info("[SHA1 HASH TEST] Match<{0}> Hash: {1} HEAD: '{2}'", (match?"TRUE":"FALSE"), sb.ToString(), head); return(sb.ToString()); }
public static string MD5HashString(string Str) { var HashBytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(Str)); StringBuilder HashString = new StringBuilder(); foreach (var HashByte in HashBytes) { HashString.Append(HashByte.ToString("x2")); } return(HashString.ToString()); }
public static string SHA(string format, params object[] args) { var data = String.Format(format, args); var sha1 = SHA1.Create(); var hash = sha1.ComputeHash(Encoding.ASCII.GetBytes(data)); var sb = new StringBuilder(); foreach (var HashByte in hash) { sb.Append(HashByte.ToString("x2")); } return(sb.ToString()); }