public ITestResult Perform() { IDigest digest = new MD4Digest(); byte[] resBuf = new byte[digest.GetDigestSize()]; string resStr; // // test 1 // digest.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec1.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 1" + SimpleTest.NewLine + " expected: " + resVec1 + SimpleTest.NewLine + " got : " + resStr); } // // test 2 // byte[] bytes = Hex.Decode(testVec2); digest.BlockUpdate(bytes, 0, bytes.Length); digest.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec2.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 2" + SimpleTest.NewLine + " expected: " + resVec2 + SimpleTest.NewLine + " got : " + resStr); } // // test 3 // bytes = Hex.Decode(testVec3); digest.BlockUpdate(bytes, 0, bytes.Length); digest.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec3.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 3" + SimpleTest.NewLine + " expected: " + resVec3 + SimpleTest.NewLine + " got : " + resStr); } // // test 4 // bytes = Hex.Decode(testVec4); digest.BlockUpdate(bytes, 0, bytes.Length); digest.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec4.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 4" + SimpleTest.NewLine + " expected: " + resVec4 + SimpleTest.NewLine + " got : " + resStr); } // // test 5 // bytes = Hex.Decode(testVec4); digest.BlockUpdate(bytes, 0, bytes.Length/2); // clone the IDigest IDigest d = new MD4Digest((MD4Digest)digest); digest.BlockUpdate(bytes, bytes.Length/2, bytes.Length - bytes.Length/2); digest.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec4.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 5" + SimpleTest.NewLine + " expected: " + resVec4 + SimpleTest.NewLine + " got : " + resStr); } d.BlockUpdate(bytes, bytes.Length/2, bytes.Length - bytes.Length/2); d.DoFinal(resBuf, 0); resStr = Hex.ToHexString(resBuf); if (!resVec4.Equals(resStr)) { return new SimpleTestResult(false, "MD4 failing standard vector test 5" + SimpleTest.NewLine + " expected: " + resVec4 + SimpleTest.NewLine + " got : " + resStr); } return new SimpleTestResult(true, Name + ": Okay"); }
public override string hash(string pw) { MD4Digest digest = new MD4Digest(); // We want Unicode here (UTF-16) byte[] password = Encoding.Unicode.GetBytes(pw); digest.BlockUpdate(password, 0, password.Length); int digestSize = digest.GetDigestSize(); byte[] hashData = new byte[digestSize]; digest.DoFinal(hashData, 0); // Convert to HEX and return return string.Concat(hashData.Select(b => b.ToString("X2"))); }