public unsafe void RandomInputs() { var rng = new Random(); var md5 = MD5.Create(); var digest = default(Md5Digest); for (var byteLength = 0; byteLength < 10000; byteLength++) { for (var i = 0; i < 5; i++) { var input = new byte[byteLength]; rng.NextBytes(input); var expected = md5.ComputeHash(input); fixed(byte *ptr = input) { UnsafeMd5.ComputeHash(ptr, input.Length, &digest); } AssertDigestsAreEqual(input, expected, ref digest); } } }
public unsafe void WriteBytes() { var input = new byte[100]; var rng = new Random(); rng.NextBytes(input); Md5Digest digest; UnsafeMd5.ComputeHash(input, out digest); var expected = digest.GetBytes(); var buffer1 = new byte[Md5Digest.SIZE + 2]; var buffer2 = new byte[Md5Digest.SIZE + 2]; fixed(byte *onePtr = buffer1) { digest.WriteBytes(&onePtr[1]); digest.WriteBytes(buffer2, 1); } const int end = Md5Digest.SIZE + 1; Assert.AreEqual(0, buffer1[0]); Assert.AreEqual(0, buffer2[0]); Assert.AreEqual(0, buffer1[end]); Assert.AreEqual(0, buffer2[end]); for (var i = 0; i < Md5Digest.SIZE; i++) { Assert.AreEqual(expected[i], buffer1[i + 1]); Assert.AreEqual(expected[i], buffer2[i + 1]); } }
public unsafe void ManagedVsUnmanaged() { var rng = new Random(); var input = new byte[100]; rng.NextBytes(input); Md5Digest managed, unmanaged; UnsafeMd5.ComputeHash(input, out managed); fixed(byte *inputPtr = input) UnsafeMd5.ComputeHash(inputPtr, input.Length, &unmanaged); Assert.AreEqual(managed, unmanaged); }