public override void Create() { // try creating ourselve using Create HashAlgorithm h = MD4.Create("MD4Managed"); Assert.IsTrue((h is MD4Managed), "MD4Managed"); }
/// Encodes the specified string. /// @param text The string to encode. /// @returns The encoded string. public string Encode(string text) { var buffer = Encoding.Default.GetBytes(text); var hash = MD4.Create().ComputeHash(buffer); return(HexCodec.GetString(hash)); }
private void Verify(ReadOnlySpan <byte> input, ReadOnlySpan <byte> expected) { Span <byte> output = stackalloc byte[expected.Length]; MD4.HashData(input, output); Assert.Equal(expected.ToArray(), output.ToArray()); }
public static ArrayList HashCrumbsInChunk(byte[] chunk) { //hash the crumbs uint nCrumbs = (uint)chunk.Length / Protocol.CrumbSize + 1; byte[] crumbBuffer = new byte[Protocol.CrumbSize]; byte[] shortHashResult; byte[] hashResult; uint start = 0; uint end = 0; MD4 md4 = new MD4(); ArrayList crumbsHashSet = new ArrayList(); while (end < chunk.Length - 1) { end = start + Protocol.CrumbSize - 1; if (end >= chunk.Length) { end = (uint)chunk.Length - 1; crumbBuffer = new byte[end - start + 1]; } Buffer.BlockCopy(chunk, (int)start, crumbBuffer, 0, (int)(end - start + 1)); hashResult = md4.GetByteHashFromBytes(crumbBuffer); shortHashResult = new byte[8]; Buffer.BlockCopy(hashResult, 0, shortHashResult, 0, 8); crumbsHashSet.Add(shortHashResult); start = end + 1; } return(crumbsHashSet); }
/// <summary> /// A method for getting a ED2K hash from a specified file path /// </summary> /// <param name="filePath">The path for the file to hash</param> /// <returns>A string containing the ED2K hash</returns> public static string GetED2K(string filePath) { string hash = String.Empty; using (FileStream file = File.OpenRead(filePath)) { byte[] buffer; const int CHUNK_SIZE = 9728000; double totalChunkCount = 0; long chunkCount = 0; int bufferLength = 0; MD4 md4 = new MD4(); List <byte> md4HashedBytes = new List <byte>(); buffer = new byte[CHUNK_SIZE]; totalChunkCount = Math.Ceiling(file.Length * 1.0 / CHUNK_SIZE); while ((bufferLength = file.Read(buffer, 0, CHUNK_SIZE)) > 0) { ++chunkCount; byte[] chunkMd4HashedBytes = MD4.GetByteHashFromBytes(buffer.Take(bufferLength).ToArray()); md4HashedBytes.AddRange(chunkMd4HashedBytes); buffer = new byte[CHUNK_SIZE]; } hash = (chunkCount > 1 ? MD4.GetHexHashFromBytes(md4HashedBytes.ToArray()) : MD4.BytesToHex(md4HashedBytes.ToArray(), md4HashedBytes.Count)).ToLower(); } return(hash); }
public static long SerializationIndexFromObjectIdentifier(ObjectIdentifier objectID) { byte[] bytes; var md4 = MD4.Create(); if (objectID.fileType == FileType.MetaAssetType || objectID.fileType == FileType.SerializedAssetType) { // TODO: Variant info // NOTE: ToString() required as unity5 used the guid as a string to hash bytes = Encoding.ASCII.GetBytes(objectID.guid.ToString()); md4.TransformBlock(bytes, 0, bytes.Length, bytes, 0); bytes = BitConverter.GetBytes((int)objectID.fileType); md4.TransformBlock(bytes, 0, bytes.Length, bytes, 0); } // Or path else { bytes = Encoding.ASCII.GetBytes(objectID.filePath); md4.TransformBlock(bytes, 0, bytes.Length, bytes, 0); } bytes = BitConverter.GetBytes(objectID.localIdentifierInFile); md4.TransformFinalBlock(bytes, 0, bytes.Length); var hash = BitConverter.ToInt64(md4.Hash, 0); return(hash); }
/// <summary> /// Derive a key from a password. /// </summary> /// <remarks>Not all encryption types are supported.</remarks> /// <param name="key_encryption">The key encryption to use.</param> /// <param name="password">The password to derice from.</param> /// <param name="iterations">Iterations for the password derivation.</param> /// <param name="name_type">The key name type.</param> /// <param name="principal">Principal for key, in form TYPE/name@realm.</param> /// <param name="salt">Salt for the key.</param> /// <param name="version">Key Version Number (KVNO).</param> /// <returns></returns> public static KerberosAuthenticationKey DeriveKey(KerberosEncryptionType key_encryption, string password, int iterations, KerberosNameType name_type, string principal, string salt, uint version) { if (principal is null) { throw new ArgumentNullException(nameof(principal)); } byte[] key; switch (key_encryption) { case KerberosEncryptionType.ARCFOUR_HMAC_MD5: case KerberosEncryptionType.ARCFOUR_HMAC_MD5_56: case KerberosEncryptionType.ARCFOUR_HMAC_OLD: case KerberosEncryptionType.ARCFOUR_HMAC_OLD_EXP: key = MD4.CalculateHash(Encoding.Unicode.GetBytes(password)); break; case KerberosEncryptionType.AES128_CTS_HMAC_SHA1_96: key = DeriveAesKey(password, MakeSalt(salt, principal), iterations, 16); break; case KerberosEncryptionType.AES256_CTS_HMAC_SHA1_96: key = DeriveAesKey(password, MakeSalt(salt, principal), iterations, 32); break; default: throw new ArgumentException($"Unsupported key type {key_encryption}", nameof(key_encryption)); } return(new KerberosAuthenticationKey(key_encryption, key, name_type, principal, DateTime.Now, version)); }
public static byte[] CreateNtlmHash(string password) { var hashCreator = new MD4(); var ntmlHash = hashCreator.Update(Encoding.Unicode.GetBytes(password)).Digest(); return(ntmlHash); }
/// <summary> /// This constructor is here to implement the clonability of this class /// </summary> /// <param name="md"> </param> private MD4(MD4 md) : this() { //this(); context = (uint[])md.context.Clone(); buffer = (byte[])md.buffer.Clone(); count = md.count; }
public static string GenerateInternalFileName(string name) { var md4 = MD4.Create(); var bytes = Encoding.ASCII.GetBytes(name); md4.TransformFinalBlock(bytes, 0, bytes.Length); return("CAB-" + BitConverter.ToString(md4.Hash, 0).ToLower().Replace("-", "")); }
/// <summary> /// Returns a binary hash from an input byte array /// </summary> /// <param name="b">byte-array to hash</param> /// <returns>binary hash of input</returns> public byte[] GetByteHashFromBytes(byte[] b) { MD4 md4 = new MD4(); md4.engineUpdate(b, 0, b.Length); return(md4.engineDigest()); }
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_d(string testName, MD4 hash, byte[] input, byte[] result) { byte[] output = hash.TransformFinalBlock(input, 0, input.Length); AssertEquals(testName + ".d.1", input, output); AssertEquals(testName + ".d.2", result, hash.Hash); // required or next operation will still return old hash hash.Initialize(); }
public static byte[] HashChunk(byte[] chunk) { byte[] resultHash; MD4 md4 = new MD4(); resultHash = md4.GetByteHashFromBytes(chunk); return(resultHash); }
public virtual void Create() { // create the default implementation HashAlgorithm h = MD4.Create(); Assert("MD4Managed", (h is MD4Managed)); // Note: will fail is default is changed in machine.config }
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(); }
/// <summary> /// Returns a byte hash from the input byte /// </summary> /// <param name="b">byte to hash</param> /// <returns>binary hash of the input byte</returns> public byte[] GetByteHashFromByte(byte b) { MD4 md4 = new MD4(); md4.engineUpdate(b); return(md4.engineDigest()); }
/// <summary> /// Returns a byte hash from a string /// </summary> /// <param name="s">string to hash</param> /// <returns>byte-array that contains the hash</returns> public byte[] GetByteHashFromString(string s) { byte[] b = Encoding.UTF8.GetBytes(s); MD4 md4 = new MD4(); md4.engineUpdate(b, 0, b.Length); return(md4.engineDigest()); }
public static byte[] NTOWFv2(string password, string user, string domain) { byte[] passwordBytes = Encoding.Unicode.GetBytes(password); byte[] key = new MD4().GetByteHashFromBytes(passwordBytes); string text = user.ToUpper() + domain; byte[] bytes = Encoding.Unicode.GetBytes(text); using HMACMD5 hmac = new HMACMD5(key); return(hmac.ComputeHash(bytes, 0, bytes.Length)); }
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(); }
private static int ComputeGuid(Type t) // TODO why does scriptable build pipeline not provide this { string hashGenerator = "s\0\0\0" + t.Namespace + t.Name; using (var md4 = MD4.Create()) { byte[] hash = md4.ComputeHash(Encoding.UTF8.GetBytes(hashGenerator)); return(BitConverter.ToInt32(hash, 0)); } }
/** * 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 RFC1320_e(string testName, MD4 hash, byte[] input, byte[] result) { byte[] copy = new byte [input.Length]; for (int i = 0; i < input.Length - 1; i++) { hash.TransformBlock(input, i, 1, copy, i); } byte[] output = hash.TransformFinalBlock(input, input.Length - 1, 1); AssertEquals(testName + ".e.1", input [input.Length - 1], output [0]); AssertEquals(testName + ".e.2", result, hash.Hash); // required or next operation will still return old hash hash.Initialize(); }
public void HashTest(string input, string expected) { // Arrange var md4 = new MD4(); var bytes = Encoding.ASCII.GetBytes(input); // Act var hash = md4.Hash(bytes); // Assert var hashString = BitConverter.ToString(hash).Replace("-", "").ToLower(); hashString.Should().Be(expected); }
public void TestNTLMv1ExtendedSessionSecurityKeyExchangeMIC() { string password = "******"; byte[] type1 = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x97, 0x82, 0x08, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x0f }; byte[] type2 = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x38, 0x00, 0x00, 0x00, 0x15, 0x82, 0x8a, 0xe2, 0x7a, 0x6d, 0x47, 0x52, 0x11, 0x8b, 0x9f, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x60, 0x00, 0x48, 0x00, 0x00, 0x00, 0x06, 0x00, 0x71, 0x17, 0x00, 0x00, 0x00, 0x0f, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x02, 0x00, 0x10, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x01, 0x00, 0x10, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x04, 0x00, 0x10, 0x00, 0x54, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x03, 0x00, 0x10, 0x00, 0x54, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x07, 0x00, 0x08, 0x00, 0x28, 0x9a, 0x19, 0xec, 0x8d, 0x92, 0xd2, 0x01, 0x00, 0x00, 0x00, 0x00 }; byte[] type3 = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x03, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x94, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x66, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0xac, 0x00, 0x00, 0x00, 0x15, 0x82, 0x88, 0xe2, 0x0a, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x0f, 0xc6, 0x21, 0x82, 0x59, 0x83, 0xda, 0xc7, 0xe7, 0xfa, 0x96, 0x44, 0x67, 0x16, 0xc3, 0xb3, 0x5b, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x36, 0x00, 0x55, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x36, 0x00, 0x90, 0x13, 0xb0, 0x36, 0xa4, 0xa5, 0xf0, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x46, 0xea, 0x77, 0x30, 0x44, 0xee, 0xa5, 0x98, 0x26, 0xa0, 0x93, 0x71, 0x5c, 0x83, 0xff, 0x76, 0x70, 0x1d, 0xf0, 0xb8, 0xa0, 0xad, 0x4d, 0xac, 0xe9, 0xf4, 0x5c, 0x3e, 0xb1, 0xb6, 0x48, 0x08, 0xa0, 0x46, 0x8c, 0x31, 0xe1, 0x2d, 0x60 }; byte[] serverChallenge = new ChallengeMessage(type2).ServerChallenge; AuthenticateMessage authenticateMessage = new AuthenticateMessage(type3); byte[] sessionBaseKey = new MD4().GetByteHashFromBytes(NTLMCryptography.NTOWFv1(password)); byte[] lmowf = NTLMCryptography.LMOWFv1(password); byte[] exportedSessionKey = GetExportedSessionKey(sessionBaseKey, authenticateMessage, serverChallenge, lmowf); // https://msdn.microsoft.com/en-us/library/cc236695.aspx const int micFieldOffset = 72; ByteWriter.WriteBytes(type3, micFieldOffset, new byte[16]); byte[] temp = ByteUtils.Concatenate(ByteUtils.Concatenate(type1, type2), type3); byte[] mic = new HMACMD5(exportedSessionKey).ComputeHash(temp); byte[] expected = new byte[] { 0xc6, 0x21, 0x82, 0x59, 0x83, 0xda, 0xc7, 0xe7, 0xfa, 0x96, 0x44, 0x67, 0x16, 0xc3, 0xb3, 0x5b }; Assert.IsTrue(ByteUtils.AreByteArraysEqual(mic, expected)); }
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); } }
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(); } }
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); }
/// <summary> /// Returns a binary hash from an input byte array /// </summary> /// <param name="b">byte-array to hash</param> /// <returns>binary hash of input</returns> public byte[] GetByteHashFromBytes(byte[] b) { MD4 md4 = new MD4(); md4.engineUpdate(b, 0, b.Length); return md4.engineDigest(); }
/// <summary> /// Returns a byte hash from a string /// </summary> /// <param name="s">string to hash</param> /// <returns>byte-array that contains the hash</returns> public byte[] GetByteHashFromString(string s) { byte[] b = Encoding.UTF8.GetBytes(s); MD4 md4 = new MD4(); md4.engineUpdate(b, 0, b.Length); return md4.engineDigest(); }
public static byte[] DoHashSetHash(ArrayList in_HashSet) { if (in_HashSet.Count==0) return null; byte[] unitedHash=new byte[16*in_HashSet.Count]; int i=0; foreach (byte[] parcialHash in in_HashSet) { // Array.Copy(HashParcial,0,HashUnido,16*i,16); Buffer.BlockCopy(parcialHash,0,unitedHash,16*i,16); i++; } byte[] resultHash=new byte[16]; MD4 md4=new MD4(); resultHash=md4.GetByteHashFromBytes(unitedHash); return resultHash; }
public static byte[] HashChunk(byte[] chunk) { byte[] resultHash; MD4 md4=new MD4(); resultHash=md4.GetByteHashFromBytes(chunk); return resultHash; }
/// <summary> /// Returns a byte hash from the input byte /// </summary> /// <param name="b">byte to hash</param> /// <returns>binary hash of the input byte</returns> public byte[] GetByteHashFromByte(byte b) { MD4 md4 = new MD4(); md4.engineUpdate(b); return md4.engineDigest(); }