Beispiel #1
0
 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();
 }
Beispiel #2
0
 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();
 }
Beispiel #3
0
        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));
            }
        }
Beispiel #4
0
        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");
            }
        }