string GenLink(string filename) { FileInfo fi = new FileInfo(filename); if (fi.Exists) { AICHHash aich = new AICHHash(fi.Length); List <byte[]> hashset = new List <byte[]>(); MD4 md4 = MD4.Create(); using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] buffer = new byte[EMPARTSIZE]; int readCount; do { readCount = fs.Read(buffer, 0, buffer.Length); if (readCount > 0) { hashset.Add(md4.ComputeHash(buffer, 0, readCount)); aich.CalcAICH(buffer, 0, readCount); if (worker != null && worker.WorkerReportsProgress) { worker.ReportProgress((int)(fs.Position * 100 / fs.Length), this); } } else if (fs.Length % EMPARTSIZE == 0) { hashset.Add(md4.ComputeHash(new byte[] { })); } }while (readCount != 0); } byte[] filehash = new byte[16]; if (hashset.Count == 1) { filehash = hashset[0]; } else { filehash = md4.ComputeHash(hashset.SelectMany(bytes => bytes).ToArray()); } return(string.Format("ed2k://|file|{0}|{1}|{2}|h={3}|/", System.Web.HttpUtility.UrlEncode(fi.Name), fi.Length, String.Concat(filehash.Select(b => b.ToString("X2")).ToArray()), aich.RootHash)); } else { throw new FileNotFoundException(); } }
public void RFC1320_b(string testName, MD4 hash, byte[] input, byte[] result) { byte[] output = hash.ComputeHash(input, 0, input.Length); AssertEquals(testName + ".b.1", result, output); AssertEquals(testName + ".b.2", result, hash.Hash); // required or next operation will still return old hash hash.Initialize(); }
private static byte[] nTOWFv1(string password) { if (password == null) { throw new Exception("Password parameter is required"); } return(MD4.ComputeHash(Encoding.Unicode.GetBytes(password))); }
public void RFC1320_c(string testName, MD4 hash, byte[] input, byte[] result) { MemoryStream ms = new MemoryStream(input); byte[] output = hash.ComputeHash(ms); AssertEquals(testName + ".c.1", result, output); AssertEquals(testName + ".c.2", result, hash.Hash); // required or next operation will still return old hash hash.Initialize(); }
/** * Creates the NTLM Hash of the user's password. * * @param password * The password. * * @return The NTLM Hash of the given password, used in the calculation of * the NTLM Response and the NTLMv2 and LMv2 Hashes. */ private byte[] CalculateNtlmHash(String password) { byte[] unicodePassword = Encoding.Unicode.GetBytes(password); byte[] hash; using (HashAlgorithm md4 = new MD4()) { hash = md4.ComputeHash(unicodePassword); } return(hash); }
/// <summary> /// Gets the NT Lan Manager (NTLM) user session key for the given initialization values of this <see cref="CipherGen"/> class. /// </summary> /// <returns>The NTLM user session key.</returns> public byte[] GetNtlmUserSessionKey() { if (ntlmUserSessionKey == null) { using (HashAlgorithm md4 = new MD4()) { ntlmUserSessionKey = md4.ComputeHash(GetNtlmHash()); } } return(ntlmUserSessionKey); }
public void TestArgumentExceptions() { using (var md4 = new MD4()) { var buffer = new byte[16]; Assert.Throws <InvalidOperationException> (() => { var x = md4.Hash; }); Assert.Throws <ArgumentNullException> (() => md4.ComputeHash((byte[])null)); Assert.Throws <ArgumentNullException> (() => md4.ComputeHash((Stream)null)); Assert.Throws <ArgumentNullException> (() => md4.ComputeHash(null, 0, buffer.Length)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.ComputeHash(buffer, -1, buffer.Length)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.ComputeHash(buffer, 0, -1)); Assert.Throws <ArgumentNullException> (() => md4.TransformBlock(null, 0, buffer.Length, buffer, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.TransformBlock(buffer, -1, buffer.Length, buffer, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.TransformBlock(buffer, 0, -1, buffer, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.TransformBlock(buffer, 0, buffer.Length, buffer, -1)); Assert.Throws <ArgumentNullException> (() => md4.TransformFinalBlock(null, 0, buffer.Length)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.TransformFinalBlock(buffer, -1, buffer.Length)); Assert.Throws <ArgumentOutOfRangeException> (() => md4.TransformFinalBlock(buffer, 0, -1)); } }
public void TestSimpleInput() { var text = Encoding.ASCII.GetBytes("This is some sample text that we will hash using the MD4 algorithm."); const string expected = "69b390afdf693eae92ebea5cc6669b3f"; using (var md4 = new MD4()) { StringBuilder builder = new StringBuilder(); byte[] hash, output = new byte[16]; Assert.AreEqual(16, md4.TransformBlock(text, 0, 16, output, 0), "TransformBlock"); output = md4.TransformFinalBlock(text, 16, text.Length - 16); Assert.NotNull(output, "TransformFinalBlock"); Assert.AreEqual(text.Length - 16, output.Length, "TransformFinalBlock"); hash = md4.Hash; Assert.NotNull(hash, "Hash"); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } Assert.AreEqual(expected, builder.ToString(), "Hash"); } using (var md4 = new MD4()) { StringBuilder builder = new StringBuilder(); var hash = md4.ComputeHash(text); Assert.NotNull(hash, "ComputeHash"); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } Assert.AreEqual(expected, builder.ToString(), "ComputeHash"); } using (var md4 = new MD4()) { StringBuilder builder = new StringBuilder(); byte[] hash; using (var stream = new MemoryStream(text, false)) hash = md4.ComputeHash(stream); Assert.NotNull(hash, "ComputeHash"); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } Assert.AreEqual(expected, builder.ToString(), "ComputeHash"); } }
public static int Compute(Type t) { string toBeHashed = "s\0\0\0" + t.Namespace + t.Name; using (HashAlgorithm hash = new MD4()) { byte[] hashed = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toBeHashed)); int result = 0; for (int i = 3; i >= 0; --i) { result <<= 8; result |= hashed[i]; } return(result); } }
static byte[] Compute_NTLM_Password(string password) { var buffer = new byte [21]; // create NT password MD4 md4 = MD4.Create(); byte[] data = ((password == null) ? (new byte [0]) : (Encoding.Unicode.GetBytes(password))); byte[] hash = md4.ComputeHash(data); Buffer.BlockCopy(hash, 0, buffer, 0, 16); // clean up Array.Clear(data, 0, data.Length); Array.Clear(hash, 0, hash.Length); return(buffer); }
private static int Compute(string @namespace, string name) { string toBeHashed = $"s\0\0\0{@namespace}{name}"; using (HashAlgorithm hash = new MD4()) { byte[] hashed = hash.ComputeHash(Encoding.UTF8.GetBytes(toBeHashed)); int result = 0; for (int i = 3; i >= 0; --i) { result <<= 8; result |= hashed[i]; } return result; } }
public static int ComputeFileID(string ns, string name) { using (HashAlgorithm hash = new MD4()) { byte[] idBytes = Encoding.UTF8.GetBytes($"s\0\0\0{ns}{name}"); byte[] hashed = hash.ComputeHash(idBytes); int result = 0; for (int i = 3; i >= 0; --i) { result <<= 8; result |= hashed[i]; } return(result); } }
/// <summary> /// Computes an NT hash value with a password using NTOWFv1. /// </summary> /// <param name="password">A password to compute hash</param> /// <exception cref="ArgumentNullException">Raised if password is null</exception> /// <returns>An NT hash value</returns> public static byte[] GetHashWithNTOWFv1(string password) { if (password == null) { // Will be changed to StackSdkException after StackSdkException class is updated. throw new ArgumentNullException("password"); } byte[] hash; using (MD4 md4 = MD4.Create()) { // Get password byte array byte[] passwordBuffer = Encoding.Unicode.GetBytes(password); hash = md4.ComputeHash(passwordBuffer); } return(hash); }
public byte[] Reflection() => md4.ComputeHash(data);