diff options
author | Tim Whittington <bc@whittington.net.nz> | 2013-10-14 09:04:52 +1300 |
---|---|---|
committer | Tim Whittington <bc@whittington.net.nz> | 2013-10-20 21:32:35 +1300 |
commit | 404ec2ebd70d6ffa68b2691bec9221e29e9161d7 (patch) | |
tree | ffddfe33c64854ff303676475612c298304384b0 /crypto | |
parent | Port SM3 digest implementation and tests from bc-java. (diff) | |
download | BouncyCastle.NET-ed25519-404ec2ebd70d6ffa68b2691bec9221e29e9161d7.tar.xz |
Refactor digest tests to subclass DigestTest, the same as bc-java.
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/test/src/crypto/test/MD2DigestTest.cs | 227 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/MD4DigestTest.cs | 203 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/MD5DigestTest.cs | 204 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RipeMD128DigestTest.cs | 177 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RipeMD160DigestTest.cs | 173 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RipeMD256DigestTest.cs | 126 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RipeMD320DigestTest.cs | 147 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/SHA1DigestTest.cs | 134 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/SHA224DigestTest.cs | 224 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/SHA256DigestTest.cs | 232 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/SHA384DigestTest.cs | 233 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/SHA512DigestTest.cs | 203 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/TigerDigestTest.cs | 106 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/WhirlpoolDigestTest.cs | 124 |
14 files changed, 644 insertions, 1869 deletions
diff --git a/crypto/test/src/crypto/test/MD2DigestTest.cs b/crypto/test/src/crypto/test/MD2DigestTest.cs index e9d2cb88f..963a57e49 100644 --- a/crypto/test/src/crypto/test/MD2DigestTest.cs +++ b/crypto/test/src/crypto/test/MD2DigestTest.cs @@ -7,198 +7,61 @@ using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; - namespace Org.BouncyCastle.Crypto.Tests { - /** + /** * standard vector test for MD2 * from RFC1319 by B.Kaliski of RSA Laboratories April 1992 * */ - [TestFixture] - public class MD2DigestTest - : ITest - { - static private string testVec1 = ""; - static private string resVec1 = "8350e5a3e24c153df2275c9f80692773"; - static private string testVec2 = "61"; - static private string resVec2 = "32ec01ec4a6dac72c0ab96fb34c0b5d1"; - static private string testVec3 = "616263"; - static private string resVec3 = "da853b0d3f88d99b30283a69e6ded6bb"; - static private string testVec4 = "6d65737361676520646967657374"; - static private string resVec4 = "ab4f496bfb2a530b219ff33031fe06b0"; - static private string testVec5 = "6162636465666768696a6b6c6d6e6f707172737475767778797a"; - static private string resVec5 = "4e8ddff3650292ab5a4108c3aa47940b"; - static private string testVec6 = "4142434445464748494a4b4c4d4e4f505152535455565758595a6162636465666768696a6b6c6d6e6f707172737475767778797a30313233343536373839"; - static private string resVec6 = "da33def2a42df13975352846c30338cd"; - static private string testVec7 = "3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930"; - static private string resVec7 = "d5976f79d83d3a0dc9806c3c66f3efd8"; - - public string Name - { - get { return "MD2"; } - } - - public ITestResult Perform() - { - IDigest digest = new MD2Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - string resStr; - - // - // test 1 - // - byte[] bytes = Hex.Decode(testVec1); - digest.BlockUpdate(bytes, 0, bytes.Length); - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec1.Equals(resStr)) - { - return new SimpleTestResult(false, - "MD2 failing standard vector test 1" - + SimpleTest.NewLine - + " expected: " + resVec1 - + SimpleTest.NewLine - + " got : " + resStr); - } - - // - // test 2 - // - 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, - "MD2 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, - "MD2 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, - "MD2 failing standard vector test 4" - + SimpleTest.NewLine - + " expected: " + resVec4 - + SimpleTest.NewLine - + " got : " + resStr); - } - // - // test 5 - // - bytes = Hex.Decode(testVec5); - - digest.BlockUpdate(bytes, 0, bytes.Length); - - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec5.Equals(resStr)) - { - return new SimpleTestResult(false, - //System.err.println( - "MD2 failing standard vector test 5" - + SimpleTest.NewLine - + " expected: " + resVec5 - + SimpleTest.NewLine - + " got : " + resStr); - } - // - // test 6 - // - bytes = Hex.Decode(testVec6); - - digest.BlockUpdate(bytes, 0, bytes.Length); - - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec6.Equals(resStr)) - { - return new SimpleTestResult(false, - "MD2 failing standard vector test 6" - + SimpleTest.NewLine - + " expected: " + resVec6 - + SimpleTest.NewLine - + " got : " + resStr); - } - // - // test 7 - // - bytes = Hex.Decode(testVec7); - - digest.BlockUpdate(bytes, 0, bytes.Length); - - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec7.Equals(resStr)) - { - return new SimpleTestResult(false, - "MD2 failing standard vector test 7" - + SimpleTest.NewLine - + " expected: " + resVec7 - + SimpleTest.NewLine - + " got : " + resStr); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } + [TestFixture] + public class MD2DigestTest + : DigestTest + { + static string[] messages = + { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + static string[] digests = + { + "8350e5a3e24c153df2275c9f80692773", + "32ec01ec4a6dac72c0ab96fb34c0b5d1", + "da853b0d3f88d99b30283a69e6ded6bb", + "ab4f496bfb2a530b219ff33031fe06b0", + "4e8ddff3650292ab5a4108c3aa47940b", + "da33def2a42df13975352846c30338cd", + "d5976f79d83d3a0dc9806c3c66f3efd8" + }; + + internal MD2DigestTest() + : base(new MD2Digest(), messages, digests) + { + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new MD2Digest((MD2Digest)digest); + } public static void Main( - string[] args) - { - ITest test = new MD2DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + string[] args) + { + RunTest(new MD2DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); Assert.AreEqual(Name + ": Okay", resultText); - } - } + } + } } diff --git a/crypto/test/src/crypto/test/MD4DigestTest.cs b/crypto/test/src/crypto/test/MD4DigestTest.cs index 89b5d7949..a4062e92d 100644 --- a/crypto/test/src/crypto/test/MD4DigestTest.cs +++ b/crypto/test/src/crypto/test/MD4DigestTest.cs @@ -9,168 +9,51 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** - * standard vector test for MD4 from RFC 1320. + /** + * standard vector test for MD4 from RFC 1320. */ - [TestFixture] - public class MD4DigestTest - : ITest - { -// static private string testVec1 = ""; - static private string resVec1 = "31d6cfe0d16ae931b73c59d7e0c089c0"; - - static private string testVec2 = "61"; - static private string resVec2 = "bde52cb31de33e46245e05fbdbd6fb24"; - - static private string testVec3 = "616263"; - static private string resVec3 = "a448017aaf21d8525fc10ae87aa6729d"; - - static private string testVec4 = "3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930"; - static private string resVec4 = "e33b4ddc9c38f2199c3e7b164fcc0536"; - - public string Name - { - get { return "MD4"; } - } - - 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 static void Main( - string[] args) - { - ITest test = new MD4DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + [TestFixture] + public class MD4DigestTest + : DigestTest + { + static private string[] messages = + { + "", + "a", + "abc", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + static private string[] digests = + { + "31d6cfe0d16ae931b73c59d7e0c089c0", + "bde52cb31de33e46245e05fbdbd6fb24", + "a448017aaf21d8525fc10ae87aa6729d", + "e33b4ddc9c38f2199c3e7b164fcc0536" + }; + + internal MD4DigestTest() + : base(new MD4Digest(), messages, digests) + { + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new MD4Digest((MD4Digest)digest); + } + + public static void Main( + string[] args) + { + RunTest(new MD4DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + public void TestFunction() + { + string resultText = Perform().ToString(); + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/MD5DigestTest.cs b/crypto/test/src/crypto/test/MD5DigestTest.cs index b38e06391..75cc9f8a4 100644 --- a/crypto/test/src/crypto/test/MD5DigestTest.cs +++ b/crypto/test/src/crypto/test/MD5DigestTest.cs @@ -7,169 +7,53 @@ using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; - namespace Org.BouncyCastle.Crypto.Tests { - /// <remarks>Standard vector test for MD5 from "Handbook of Applied Cryptography", page 345.</remarks> - [TestFixture] - public class MD5DigestTest - : ITest - { -// static private string testVec1 = ""; - static private string resVec1 = "d41d8cd98f00b204e9800998ecf8427e"; - - static private string testVec2 = "61"; - static private string resVec2 = "0cc175b9c0f1b6a831c399e269772661"; - - static private string testVec3 = "616263"; - static private string resVec3 = "900150983cd24fb0d6963f7d28e17f72"; - - static private string testVec4 = "6162636465666768696a6b6c6d6e6f707172737475767778797a"; - static private string resVec4 = "c3fcd3d76192e4007dfb496cca67e13b"; - - public string Name - { - get { return "MD5"; } - } - - public ITestResult Perform() - { - IDigest digest = new MD5Digest(); - 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, - "MD5 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, - "MD5 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, - "MD5 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, - "MD5 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 MD5Digest((MD5Digest)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, - "MD5 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, - "MD5 failing standard vector test 5" - + SimpleTest.NewLine - + " expected: " + resVec4 - + SimpleTest.NewLine - + " got : " + resStr); - } - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( - string[] args) - { - ITest test = new MD5DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + /** + * standard vector test for MD5 from "Handbook of Applied Cryptography", page 345. + */ + [TestFixture] + public class MD5DigestTest + : DigestTest + { + static string[] messages = + { + "", + "a", + "abc", + "abcdefghijklmnopqrstuvwxyz" + }; + + static string[] digests = + { + "d41d8cd98f00b204e9800998ecf8427e", + "0cc175b9c0f1b6a831c399e269772661", + "900150983cd24fb0d6963f7d28e17f72", + "c3fcd3d76192e4007dfb496cca67e13b" + }; + + internal MD5DigestTest() + : base(new MD5Digest(), messages, digests) + { + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new MD5Digest((MD5Digest)digest); + } + + public static void Main( + string[] args) + { + RunTest(new MD5DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + public void TestFunction() + { + string resultText = Perform().ToString(); + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/RipeMD128DigestTest.cs b/crypto/test/src/crypto/test/RipeMD128DigestTest.cs index 61d70f3f8..8cf0d7e0c 100644 --- a/crypto/test/src/crypto/test/RipeMD128DigestTest.cs +++ b/crypto/test/src/crypto/test/RipeMD128DigestTest.cs @@ -1,135 +1,74 @@ using System; -using System.Text; using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** - * RipeMD128 IDigest Test - */ - [TestFixture] - public class RipeMD128DigestTest: ITest - { - readonly static string[] messages = - { - "", - "a", - "abc", - "message digest", - "abcdefghijklmnopqrstuvwxyz", - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "12345678901234567890123456789012345678901234567890123456789012345678901234567890" - }; - - readonly static string[] digests = { - "cdf26213a150dc3ecb610f18f6b38b46", - "86be7afa339d0fc7cfc785e72f578d33", - "c14a12199c66e4ba84636b0f69144c77", - "9e327b3d6e523062afc1132d7df9d1b8", - "fd2aa607f71dc8f510714922b371834e", - "a1aa0689d0fafa2ddc22e88b49133a06", - "d1e959eb179c911faea4624c60c5c702", - "3f45ef194732c2dbb2c4a2c769795fa3" - }; - - readonly static string MillionADigest = "4a7f5723f954eba1216c9d8f6320431f"; - - public string Name - { - get { return "RipeMD128"; } - } - - public ITestResult Perform() - { - IDigest digest = new RipeMD128Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - - for (int i = 0; i < messages.Length; i++) - { - byte[] m = Encoding.ASCII.GetBytes(messages[i]); - digest.BlockUpdate(m, 0, m.Length); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) - { - return new SimpleTestResult(false, Name + ": Vector " + i + " failed"); - } - } - - // - // test 2 - // - byte[] mm = Encoding.ASCII.GetBytes(messages[messages.Length-1]); - - digest.BlockUpdate(mm, 0, mm.Length/2); - - // clone the IDigest - IDigest d = new RipeMD128Digest((RipeMD128Digest)digest); - - digest.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "RipeMD128 failing clone test" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } - - d.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - d.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "RipeMD128 failing clone test - part 2" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } - - for (int i = 0; i < 1000000; i++) - { - digest.Update((byte)'a'); - } - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(MillionADigest))) - { - return new SimpleTestResult(false, Name + ": Million a's failed"); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( + /** + * RIPEMD128 Digest Test + */ + [TestFixture] + public class RipeMD128DigestTest + : DigestTest + { + readonly static string[] messages = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + readonly static string[] digests = { + "cdf26213a150dc3ecb610f18f6b38b46", + "86be7afa339d0fc7cfc785e72f578d33", + "c14a12199c66e4ba84636b0f69144c77", + "9e327b3d6e523062afc1132d7df9d1b8", + "fd2aa607f71dc8f510714922b371834e", + "a1aa0689d0fafa2ddc22e88b49133a06", + "d1e959eb179c911faea4624c60c5c702", + "3f45ef194732c2dbb2c4a2c769795fa3" + }; + + readonly static String million_a_digest = "4a7f5723f954eba1216c9d8f6320431f"; + + internal RipeMD128DigestTest() + : base(new RipeMD128Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new RipeMD128Digest((RipeMD128Digest)digest); + } + + public static void Main( string[] args) - { - ITest test = new RipeMD128DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + { + RunTest(new RipeMD128DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + public void TestFunction() + { + string resultText = Perform().ToString(); + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/RipeMD160DigestTest.cs b/crypto/test/src/crypto/test/RipeMD160DigestTest.cs index 347c64d49..76f472574 100644 --- a/crypto/test/src/crypto/test/RipeMD160DigestTest.cs +++ b/crypto/test/src/crypto/test/RipeMD160DigestTest.cs @@ -1,135 +1,74 @@ using System; -using System.Text; using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** - * RipeMD160 IDigest Test - */ - [TestFixture] - public class RipeMD160DigestTest - : ITest - { - readonly static string[] messages = - { - "", - "a", - "abc", - "message digest", - "abcdefghijklmnopqrstuvwxyz", - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "12345678901234567890123456789012345678901234567890123456789012345678901234567890" - }; - - readonly static string[] digests = { - "9c1185a5c5e9fc54612808977ee8f548b2258d31", - "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", - "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", - "5d0689ef49d2fae572b881b123a85ffa21595f36", - "f71c27109c692c1b56bbdceb5b9d2865b3708dbc", - "12a053384a9c0c88e405a06c27dcf49ada62eb2b", - "b0e20b6e3116640286ed3a87a5713079b21f5189", - "9b752e45573d4b39f4dbd3323cab82bf63326bfb" - }; - - readonly static string MillionADigest = "52783243c1697bdbe16d37f97f68f08325dc1528"; - - public string Name - { - get { return "RipeMD160"; } - } - - public ITestResult Perform() - { - IDigest digest = new RipeMD160Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - - for (int i = 0; i < messages.Length; i++) - { - byte[] m = Encoding.ASCII.GetBytes(messages[i]); - digest.BlockUpdate(m, 0, m.Length); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) - { - return new SimpleTestResult(false, Name + ": Vector " + i + " failed"); - } - } - - // - // test 2 - // - byte[] mm = Encoding.ASCII.GetBytes(messages[messages.Length-1]); - - digest.BlockUpdate(mm, 0, mm.Length/2); - - // clone the IDigest - IDigest d = new RipeMD160Digest((RipeMD160Digest)digest); - - digest.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "RipeMD160 failing clone test" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } - - d.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - d.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "RipeMD160 failing clone test - part 2" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } - - for (int i = 0; i < 1000000; i++) - { - digest.Update((byte)'a'); - } - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(MillionADigest))) - { - return new SimpleTestResult(false, Name + ": Million a's failed"); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } + /** + * RIPEMD160 Digest Test + */ + [TestFixture] + public class RipeMD160DigestTest + : DigestTest + { + readonly static string[] messages = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + readonly static string[] digests = { + "9c1185a5c5e9fc54612808977ee8f548b2258d31", + "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", + "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", + "5d0689ef49d2fae572b881b123a85ffa21595f36", + "f71c27109c692c1b56bbdceb5b9d2865b3708dbc", + "12a053384a9c0c88e405a06c27dcf49ada62eb2b", + "b0e20b6e3116640286ed3a87a5713079b21f5189", + "9b752e45573d4b39f4dbd3323cab82bf63326bfb" + }; + + readonly static string million_a_digest = "52783243c1697bdbe16d37f97f68f08325dc1528"; + + internal RipeMD160DigestTest() + : base(new RipeMD160Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new RipeMD160Digest((RipeMD160Digest)digest); + } public static void Main( string[] args) - { - ITest test = new RipeMD160DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + { + RunTest(new RipeMD160DigestTest()); + } - [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); Assert.AreEqual(Name + ": Okay", resultText); - } - } + } + } } diff --git a/crypto/test/src/crypto/test/RipeMD256DigestTest.cs b/crypto/test/src/crypto/test/RipeMD256DigestTest.cs index 909200d64..27a9c88cc 100644 --- a/crypto/test/src/crypto/test/RipeMD256DigestTest.cs +++ b/crypto/test/src/crypto/test/RipeMD256DigestTest.cs @@ -1,97 +1,67 @@ using System; -using System.Text; using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /// <summary> RipeMD256 IDigest Test</summary> - [TestFixture] - public class RipeMD256DigestTest - : ITest - { - public string Name - { - get { return "RipeMD256"; } - } - - internal static readonly string[] messages = new string[]{"", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"}; - - internal static readonly string[] digests = new string[]{"02ba4c4e5f8ecd1877fc52d64d30e37a2d9774fb1e5d026380ae0168e3c5522d", "f9333e45d857f5d90a91bab70a1eba0cfb1be4b0783c9acfcd883a9134692925", "afbd6e228b9d8cbbcef5ca2d03e6dba10ac0bc7dcbe4680e1e42d2e975459b65", "87e971759a1ce47a514d5c914c392c9018c7c46bc14465554afcdf54a5070c0e", "649d3034751ea216776bf9a18acc81bc7896118a5197968782dd1fd97d8d5133", "3843045583aac6c8c8d9128573e7a9809afb2a0f34ccc36ea9e72f16f6368e3f", "5740a408ac16b720b84424ae931cbb1fe363d1d0bf4017f1a89f7ea6de77a0b8", "06fdcc7a409548aaf91368c06a6275b553e3f099bf0ea4edfd6778df89a890dd"}; - - internal const string MillionADigest = "ac953744e10e31514c150d4d8d7b677342e33399788296e43ae4850ce4f97978"; - - public virtual ITestResult Perform() - { - IDigest digest = new RipeMD256Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - - for (int i = 0; i < messages.Length; i++) - { - byte[] m = Encoding.ASCII.GetBytes(messages[i]); - digest.BlockUpdate(m, 0, m.Length); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) - { - return new SimpleTestResult(false, Name + ": Vector " + i + " failed" + " expected: " + digests[i] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - } - } - - // - // test 2 - // - byte[] m2 = Encoding.ASCII.GetBytes(messages[messages.Length - 1]); - digest.BlockUpdate(m2, 0, m2.Length / 2); - - // clone the IDigest - IDigest d = new RipeMD256Digest((RipeMD256Digest) digest); - - digest.BlockUpdate(m2, m2.Length / 2, m2.Length - m2.Length / 2); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length - 1]))) - { - return new SimpleTestResult(false, "RipeMD256 failing clone test" + SimpleTest.NewLine + " expected: " + digests[digests.Length - 1] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - } - - d.BlockUpdate(m2, m2.Length / 2, m2.Length - m2.Length / 2); - d.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length - 1]))) - { - return new SimpleTestResult(false, "RipeMD256 failing clone test - part 2" + SimpleTest.NewLine + " expected: " + digests[digests.Length - 1] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - } - - for (int i = 0; i < 1000000; i++) - { - digest.Update((byte) 'a'); - } + /** + * RIPEMD256 Digest Test + */ + [TestFixture] + public class RipeMD256DigestTest + : DigestTest + { + readonly static string[] messages = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + readonly static string[] digests = { + "02ba4c4e5f8ecd1877fc52d64d30e37a2d9774fb1e5d026380ae0168e3c5522d", + "f9333e45d857f5d90a91bab70a1eba0cfb1be4b0783c9acfcd883a9134692925", + "afbd6e228b9d8cbbcef5ca2d03e6dba10ac0bc7dcbe4680e1e42d2e975459b65", + "87e971759a1ce47a514d5c914c392c9018c7c46bc14465554afcdf54a5070c0e", + "649d3034751ea216776bf9a18acc81bc7896118a5197968782dd1fd97d8d5133", + "3843045583aac6c8c8d9128573e7a9809afb2a0f34ccc36ea9e72f16f6368e3f", + "5740a408ac16b720b84424ae931cbb1fe363d1d0bf4017f1a89f7ea6de77a0b8", + "06fdcc7a409548aaf91368c06a6275b553e3f099bf0ea4edfd6778df89a890dd" + }; + + readonly static string million_a_digest = "ac953744e10e31514c150d4d8d7b677342e33399788296e43ae4850ce4f97978"; + + internal RipeMD256DigestTest() + : base(new RipeMD256Digest(), messages, digests) + { + } - digest.DoFinal(resBuf, 0); + public override void PerformTest() + { + base.PerformTest(); - if (!Arrays.AreEqual(resBuf, Hex.Decode(MillionADigest))) - { - return new SimpleTestResult(false, Name + ": Million a's failed"); - } + millionATest(million_a_digest); + } - return new SimpleTestResult(true, Name + ": Okay"); - } + protected override IDigest CloneDigest(IDigest digest) + { + return new RipeMD256Digest((RipeMD256Digest)digest); + } public static void Main( string[] args) - { - ITest test = new RipeMD256DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + { + RunTest(new RipeMD256DigestTest()); + } [Test] public void TestFunction() @@ -100,5 +70,5 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.AreEqual(Name + ": Okay", resultText); } - } + } } diff --git a/crypto/test/src/crypto/test/RipeMD320DigestTest.cs b/crypto/test/src/crypto/test/RipeMD320DigestTest.cs index 253cb9d9a..51296e55b 100644 --- a/crypto/test/src/crypto/test/RipeMD320DigestTest.cs +++ b/crypto/test/src/crypto/test/RipeMD320DigestTest.cs @@ -1,105 +1,74 @@ using System; -using System.Text; using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /// <summary> RipeMD320 IDigest Test</summary> - [TestFixture] - public class RipeMD320DigestTest - : ITest - { - public string Name - { - get { return "RipeMD320"; } - } - - internal static readonly string[] messages = new string[]{"", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"}; - - internal static readonly string[] digests = new string[]{"22d65d5661536cdc75c1fdf5c6de7b41b9f27325ebc61e8557177d705a0ec880151c3a32a00899b8", "ce78850638f92658a5a585097579926dda667a5716562cfcf6fbe77f63542f99b04705d6970dff5d", "de4c01b3054f8930a79d09ae738e92301e5a17085beffdc1b8d116713e74f82fa942d64cdbc4682d", "3a8e28502ed45d422f68844f9dd316e7b98533fa3f2a91d29f84d425c88d6b4eff727df66a7c0197", "cabdb1810b92470a2093aa6bce05952c28348cf43ff60841975166bb40ed234004b8824463e6b009", "d034a7950cf722021ba4b84df769a5de2060e259df4c9bb4a4268c0e935bbc7470a969c9d072a1ac", "ed544940c86d67f250d232c30b7b3e5770e0c60c8cb9a4cafe3b11388af9920e1b99230b843c86a4", "557888af5f6d8ed62ab66945c6d2a0a47ecd5341e915eb8fea1d0524955f825dc717e4a008ab2d42"}; - - internal const string MillionADigest = "bdee37f4371e20646b8b0d862dda16292ae36f40965e8c8509e63d1dbddecc503e2b63eb9245bb66"; - - public virtual ITestResult Perform() - { - IDigest digest = new RipeMD320Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - - for (int i = 0; i < messages.Length; i++) - { - byte[] m = Encoding.ASCII.GetBytes(messages[i]); - digest.BlockUpdate(m, 0, m.Length); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) - { - Console.WriteLine(Name + ": Vector " + i + " failed" + " expected: " + digests[i] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - return new SimpleTestResult(false, Name + ": Vector " + i + " failed"); - } - } - - // - // test 2 - // - byte[] m2 = Encoding.ASCII.GetBytes(messages[messages.Length - 1]); - - digest.BlockUpdate(m2, 0, m2.Length / 2); - - // clone the IDigest - IDigest d = new RipeMD320Digest((RipeMD320Digest) digest); - - digest.BlockUpdate(m2, m2.Length / 2, m2.Length - m2.Length / 2); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length - 1]))) - { - return new SimpleTestResult(false, "RipeMD320 failing clone test" + SimpleTest.NewLine + " expected: " + digests[digests.Length - 1] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - } - - d.BlockUpdate(m2, m2.Length / 2, m2.Length - m2.Length / 2); - d.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length - 1]))) - { - return new SimpleTestResult(false, "RipeMD320 failing clone test - part 2" + SimpleTest.NewLine + " expected: " + digests[digests.Length - 1] + SimpleTest.NewLine + " got : " + Hex.ToHexString(resBuf)); - } - - for (int i = 0; i < 1000000; i++) - { - digest.Update((byte) 'a'); - } - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(MillionADigest))) - { - return new SimpleTestResult(false, Name + ": Million a's failed"); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( + /** + * RIPEMD320 Digest Test + */ + [TestFixture] + public class RipeMD320DigestTest + : DigestTest + { + readonly static string[] messages = { + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890" + }; + + readonly static string[] digests = { + "22d65d5661536cdc75c1fdf5c6de7b41b9f27325ebc61e8557177d705a0ec880151c3a32a00899b8", + "ce78850638f92658a5a585097579926dda667a5716562cfcf6fbe77f63542f99b04705d6970dff5d", + "de4c01b3054f8930a79d09ae738e92301e5a17085beffdc1b8d116713e74f82fa942d64cdbc4682d", + "3a8e28502ed45d422f68844f9dd316e7b98533fa3f2a91d29f84d425c88d6b4eff727df66a7c0197", + "cabdb1810b92470a2093aa6bce05952c28348cf43ff60841975166bb40ed234004b8824463e6b009", + "d034a7950cf722021ba4b84df769a5de2060e259df4c9bb4a4268c0e935bbc7470a969c9d072a1ac", + "ed544940c86d67f250d232c30b7b3e5770e0c60c8cb9a4cafe3b11388af9920e1b99230b843c86a4", + "557888af5f6d8ed62ab66945c6d2a0a47ecd5341e915eb8fea1d0524955f825dc717e4a008ab2d42" + }; + + readonly static string million_a_digest = "bdee37f4371e20646b8b0d862dda16292ae36f40965e8c8509e63d1dbddecc503e2b63eb9245bb66"; + + internal RipeMD320DigestTest() + : base(new RipeMD320Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new RipeMD320Digest((RipeMD320Digest)digest); + } + + public static void Main( string[] args) - { - ITest test = new RipeMD320DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + { + RunTest(new RipeMD320DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); - Assert.AreEqual(Name + ": Okay", resultText); - } - } + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/SHA1DigestTest.cs b/crypto/test/src/crypto/test/SHA1DigestTest.cs index 22a5c8f46..8e0564dd8 100644 --- a/crypto/test/src/crypto/test/SHA1DigestTest.cs +++ b/crypto/test/src/crypto/test/SHA1DigestTest.cs @@ -12,126 +12,32 @@ namespace Org.BouncyCastle.Crypto.Tests /// <remarks>Standard vector test for SHA-1 from "Handbook of Applied Cryptography", page 345.</remarks> [TestFixture] public class Sha1DigestTest - : SimpleTest + : DigestTest { - //static private string testVec1 = ""; - static private string resVec1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; - - static private string testVec2 = "61"; - static private string resVec2 = "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"; - - static private string testVec3 = "616263"; - static private string resVec3 = "a9993e364706816aba3e25717850c26c9cd0d89d"; - - static private string testVec4 = "6162636465666768696a6b6c6d6e6f707172737475767778797a"; - static private string resVec4 = "32d10c7b8cf96570ca04ce37f2a19d84240d3a89"; + private static string[] messages = + { + "", + "a", + "abc", + "abcdefghijklmnopqrstuvwxyz" + }; - public override string Name + private static string[] digests = + { + "da39a3ee5e6b4b0d3255bfef95601890afd80709", + "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", + "a9993e364706816aba3e25717850c26c9cd0d89d", + "32d10c7b8cf96570ca04ce37f2a19d84240d3a89" + }; + + internal Sha1DigestTest() + : base(new Sha1Digest(), messages, digests) { - get { return "SHA1"; } } - public override void PerformTest() + protected override IDigest CloneDigest(IDigest digest) { - IDigest digest = new Sha1Digest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - string resStr; - - // - // test 1 - // - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec1.Equals(resStr)) - { - Fail("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)) - { - Fail("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)) - { - Fail("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)) - { - Fail("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 Sha1Digest((Sha1Digest)digest); - - digest.BlockUpdate(bytes, bytes.Length / 2, bytes.Length - bytes.Length / 2); - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec4.Equals(resStr)) - { - Fail("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)) - { - Fail("failing standard vector test 5" + SimpleTest.NewLine - + " expected: " + resVec4 + SimpleTest.NewLine - + " got : " + resStr); - } + return new Sha1Digest((Sha1Digest)digest); } public static void Main( diff --git a/crypto/test/src/crypto/test/SHA224DigestTest.cs b/crypto/test/src/crypto/test/SHA224DigestTest.cs index 37035c30f..8207a18c7 100644 --- a/crypto/test/src/crypto/test/SHA224DigestTest.cs +++ b/crypto/test/src/crypto/test/SHA224DigestTest.cs @@ -9,192 +9,62 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** - * standard vector test for SHA-224 from RFC 3874 - only the last three are in - * the RFC. - */ - [TestFixture] - public class Sha224DigestTest - : ITest - { - private const string testVec1 = ""; - private const string resVec1 = "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"; - - private const string testVec2 = "61"; - private const string resVec2 = "abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5"; - - private const string testVec3 = "616263"; - private const string resVec3 = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; - - private const string testVec4 = "6162636462636465636465666465666765666768666768696768696a68696a6b696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071"; - private const string resVec4 = "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"; + /** + * standard vector test for SHA-224 from RFC 3874 - only the last three are in + * the RFC. + */ + [TestFixture] + public class Sha224DigestTest + : DigestTest + { + private static string[] messages = + { + "", + "a", + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + }; + + private static string[] digests = + { + "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", + "abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5", + "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", + "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" + }; // 1 million 'a' - private const string testVec5 = "61616161616161616161"; - private const string resVec5 = "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"; - - public string Name - { - get { return "SHA224"; } - } - - public ITestResult Perform() - { - IDigest digest = new Sha224Digest(); - 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, - "SHA-224 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, - "SHA-224 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, - "SHA-224 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, - "SHA-224 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 Sha224Digest((Sha224Digest)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, - "SHA-224 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, - "SHA-224 failing standard vector test 5" - + SimpleTest.NewLine - + " expected: " + resVec4 - + SimpleTest.NewLine - + " got : " + resStr); - } + private static string million_a_digest = "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"; - // test 6 - bytes = Hex.Decode(testVec5); - for ( int i = 0; i < 100000; i++ ) - { - digest.BlockUpdate(bytes, 0, bytes.Length); - } - digest.DoFinal(resBuf, 0); + internal Sha224DigestTest() + : base(new Sha224Digest(), messages, digests) + { + } - resStr = Hex.ToHexString(resBuf); - if (!resVec5.Equals(resStr)) - { - return new SimpleTestResult(false, - "SHA-224 failing standard vector test 6" - + SimpleTest.NewLine - + " expected: " + resVec5 - + SimpleTest.NewLine - + " got : " + resStr); - } + public override void PerformTest() + { + base.PerformTest(); - return new SimpleTestResult(true, Name + ": Okay"); - } + millionATest(million_a_digest); + } - public static void Main( - string[] args) - { - ITest test = new Sha224DigestTest(); - ITestResult result = test.Perform(); + protected override IDigest CloneDigest(IDigest digest) + { + return new Sha224Digest((Sha224Digest)digest); + } - Console.WriteLine(result); - } + public static void Main( + string[] args) + { + RunTest(new Sha224DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); Assert.AreEqual(Name + ": Okay", resultText); - } - } + } + } } diff --git a/crypto/test/src/crypto/test/SHA256DigestTest.cs b/crypto/test/src/crypto/test/SHA256DigestTest.cs index 67f0c2397..d4d29ccbb 100644 --- a/crypto/test/src/crypto/test/SHA256DigestTest.cs +++ b/crypto/test/src/crypto/test/SHA256DigestTest.cs @@ -4,202 +4,68 @@ using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Encodings; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** + /** * standard vector test for SHA-256 from FIPS Draft 180-2. * * Note, the first two vectors are _not_ from the draft, the last three are. */ - [TestFixture] - public class Sha256DigestTest - : ITest - { -// static private string testVec1 = ""; - static private string resVec1 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; - - static private string testVec2 = "61"; - static private string resVec2 = "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"; - - static private string testVec3 = "616263"; - static private string resVec3 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; - - static private string testVec4 = "6162636462636465636465666465666765666768666768696768696a68696a6b696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071"; - static private string resVec4 = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"; - - // 1 million 'a' - static private string testVec5 = "61616161616161616161"; - static private string resVec5 = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"; - - public string Name - { - get { return "SHA256"; } - } - - public ITestResult Perform() - { - IDigest digest = new Sha256Digest(); - 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, - "SHA-256 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, - "SHA-256 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, - "SHA-256 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, - "SHA-256 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 Sha256Digest((Sha256Digest)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, - "SHA-256 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, - "SHA-256 failing standard vector test 5" - + SimpleTest.NewLine - + " expected: " + resVec4 - + SimpleTest.NewLine - + " got : " + resStr); - } - - // test 6 - bytes = Hex.Decode(testVec5); - for ( int i = 0; i < 100000; i++ ) - { - digest.BlockUpdate(bytes, 0, bytes.Length); - } - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec5.Equals(resStr)) - { - return new SimpleTestResult(false, - "SHA-256 failing standard vector test 6" - + SimpleTest.NewLine - + " expected: " + resVec5 - + SimpleTest.NewLine - + " got : " + resStr); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } + [TestFixture] + public class Sha256DigestTest + : DigestTest + { + private static string[] messages = + { + "", + "a", + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + }; + + private static string[] digests = + { + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" + }; + + // 1 million 'a' + static private string million_a_digest = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"; + + internal Sha256DigestTest() + : base(new Sha256Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new Sha256Digest((Sha256Digest)digest); + } public static void Main( string[] args) - { - ITest test = new Sha256DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + { + RunTest(new Sha256DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + public void TestFunction() + { + string resultText = Perform().ToString(); + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/SHA384DigestTest.cs b/crypto/test/src/crypto/test/SHA384DigestTest.cs index 70c26edcc..ae9ad0c17 100644 --- a/crypto/test/src/crypto/test/SHA384DigestTest.cs +++ b/crypto/test/src/crypto/test/SHA384DigestTest.cs @@ -4,200 +4,67 @@ using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; -using Org.BouncyCastle.Crypto.Engines; -using Org.BouncyCastle.Crypto.Encodings; -using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** + /** * standard vector test for SHA-384 from FIPS Draft 180-2. * * Note, the first two vectors are _not_ from the draft, the last three are. */ - [TestFixture] - public class Sha384DigestTest - : ITest - { -// static private string testVec1 = ""; - static private string resVec1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"; - - static private string testVec2 = "61"; - static private string resVec2 = "54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31"; - - static private string testVec3 = "616263"; - static private string resVec3 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"; - - static private string testVec4 = "61626364656667686263646566676869636465666768696a6465666768696a6b65666768696a6b6c666768696a6b6c6d6768696a6b6c6d6e68696a6b6c6d6e6f696a6b6c6d6e6f706a6b6c6d6e6f70716b6c6d6e6f7071726c6d6e6f707172736d6e6f70717273746e6f707172737475"; - static private string resVec4 = "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"; - - // 1 million 'a' - static private string testVec5 = "61616161616161616161"; - static private string resVec5 = "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"; - - public string Name - { - get { return "SHA384"; } - } - - public ITestResult Perform() - { - IDigest digest = new Sha384Digest(); - 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, - "SHA-384 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, - "SHA-384 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, - "SHA-384 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, - "SHA-384 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 Sha384Digest((Sha384Digest)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, - "SHA-384 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, - "SHA-384 failing standard vector test 5" - + SimpleTest.NewLine - + " expected: " + resVec4 - + SimpleTest.NewLine - + " got : " + resStr); - } - - // test 6 - bytes = Hex.Decode(testVec5); - for ( int i = 0; i < 100000; i++ ) - { - digest.BlockUpdate(bytes, 0, bytes.Length); - } - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec5.Equals(resStr)) - { - return new SimpleTestResult(false, - "SHA-384 failing standard vector test 6" - + SimpleTest.NewLine - + " expected: " + resVec5 - + SimpleTest.NewLine - + " got : " + resStr); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( - string[] args) - { - ITest test = new Sha384DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + [TestFixture] + public class Sha384DigestTest + : DigestTest + { + private static string[] messages = + { + "", + "a", + "abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + }; + + private static string[] digests = + { + "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", + "54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31", + "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", + "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039" + }; + + static private string million_a_digest = "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"; + + internal Sha384DigestTest() + : base(new Sha384Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new Sha384Digest((Sha384Digest)digest); + } + + public static void Main( + string[] args) + { + RunTest(new Sha384DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); Assert.AreEqual(Name + ": Okay", resultText); - } - } + } + } } diff --git a/crypto/test/src/crypto/test/SHA512DigestTest.cs b/crypto/test/src/crypto/test/SHA512DigestTest.cs index c8096b7db..814500b63 100644 --- a/crypto/test/src/crypto/test/SHA512DigestTest.cs +++ b/crypto/test/src/crypto/test/SHA512DigestTest.cs @@ -9,157 +9,62 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /// <summary> - /// Standard vector test for SHA-512 from FIPS Draft 180-2. - /// Note, the first two vectors are _not_ from the draft, the last three are. - /// </summary> - [TestFixture] - public class Sha512DigestTest - : ITest - { - public string Name - { - get { return "SHA512"; } - } - - //private static string testVec1 = ""; - private static string resVec1 = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"; - - private static string testVec2 = "61"; - private static string resVec2 = "1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75"; - - private static string testVec3 = "616263"; - private static string resVec3 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; - - private static string testVec4 = "61626364656667686263646566676869636465666768696a6465666768696a6b65666768696a6b6c666768696a6b6c6d6768696a6b6c6d6e68696a6b6c6d6e6f696a6b6c6d6e6f706a6b6c6d6e6f70716b6c6d6e6f7071726c6d6e6f707172736d6e6f70717273746e6f707172737475"; - private static string resVec4 = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"; - - // 1 million 'a' - private static string testVec5 = "61616161616161616161"; - private static string resVec5 = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"; - - public virtual ITestResult Perform() - { - IDigest digest = new Sha512Digest(); - 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, "SHA-512 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, "SHA-512 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, "SHA-512 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, "SHA-512 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 Sha512Digest((Sha512Digest)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, "SHA-512 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, "SHA-512 failing standard vector test 5" + SimpleTest.NewLine + " expected: " + resVec4 + SimpleTest.NewLine + " got : " + resStr); - } - - // test 6 - bytes = Hex.Decode(testVec5); - for (int i = 0; i < 100000; i++) - { - digest.BlockUpdate(bytes, 0, bytes.Length); - } - digest.DoFinal(resBuf, 0); - - resStr = Hex.ToHexString(resBuf); - if (!resVec5.Equals(resStr)) - { - return new SimpleTestResult(false, "SHA-512 failing standard vector test 6" + SimpleTest.NewLine + " expected: " + resVec5 + SimpleTest.NewLine + " got : " + resStr); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( - string[] args) - { - ITest test = new Sha512DigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + /// <summary> + /// Standard vector test for SHA-512 from FIPS Draft 180-2. + /// Note, the first two vectors are _not_ from the draft, the last three are. + /// </summary> + [TestFixture] + public class Sha512DigestTest + : DigestTest + { + private static string[] messages = + { + "", + "a", + "abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + }; + + private static string[] digests = + { + "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", + "1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75", + "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", + "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" + }; + + // 1 million 'a' + static private string million_a_digest = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"; + + internal Sha512DigestTest() + : base(new Sha512Digest(), messages, digests) + { + } + + public override void PerformTest() + { + base.PerformTest(); + + millionATest(million_a_digest); + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new Sha512Digest((Sha512Digest)digest); + } + + public static void Main( + string[] args) + { + RunTest(new Sha512DigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); Assert.AreEqual(Name + ": Okay", resultText); - } - } + } + } } diff --git a/crypto/test/src/crypto/test/TigerDigestTest.cs b/crypto/test/src/crypto/test/TigerDigestTest.cs index 6fec60185..21bcfe78d 100644 --- a/crypto/test/src/crypto/test/TigerDigestTest.cs +++ b/crypto/test/src/crypto/test/TigerDigestTest.cs @@ -20,7 +20,7 @@ namespace Org.BouncyCastle.Crypto.Tests */ [TestFixture] public class TigerDigestTest - : ITest + : DigestTest { readonly static string[] messages = { @@ -48,93 +48,35 @@ namespace Org.BouncyCastle.Crypto.Tests readonly static string hash64k = "FDF4F5B35139F48E710E421BE5AF411DE1A8AAC333F26204"; - public string Name - { - get { return "Tiger"; } - } - - public ITestResult Perform() - { - IDigest digest = new TigerDigest(); - byte[] resBuf = new byte[digest.GetDigestSize()]; - - for (int i = 0; i < messages.Length; i++) - { - byte[] m = Encoding.ASCII.GetBytes(messages[i]); - digest.BlockUpdate(m, 0, m.Length); - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i]))) - { - return new SimpleTestResult(false, Name + ": Vector " + i + " failed got " + Hex.ToHexString(resBuf)); - } - } - - // - // test 2 - // - byte[] mm = Encoding.ASCII.GetBytes(messages[messages.Length-1]); - - digest.BlockUpdate(mm, 0, mm.Length/2); - - // clone the IDigest - IDigest d = new TigerDigest((TigerDigest)digest); + internal TigerDigestTest() + : base(new TigerDigest(), messages, digests) + { + } - digest.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - digest.DoFinal(resBuf, 0); + public override void PerformTest() + { + base.PerformTest(); - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "Tiger failing clone test" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } + sixtyFourKTest(hash64k); + } - d.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2); - d.DoFinal(resBuf, 0); + protected override IDigest CloneDigest(IDigest digest) + { + return new TigerDigest((TigerDigest)digest); + } - if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1]))) - { - return new SimpleTestResult(false, - "Tiger failing clone test - part 2" - + SimpleTest.NewLine - + " expected: " + digests[digests.Length-1] - + SimpleTest.NewLine - + " got : " + Hex.ToHexString(resBuf)); - } - - for (int i = 0; i < 65536; i++) - { - digest.Update((byte)(i & 0xff)); - } - digest.DoFinal(resBuf, 0); - - if (!Arrays.AreEqual(resBuf, Hex.Decode(hash64k))) - { - return new SimpleTestResult(false, Name + ": Million a's failed"); - } - - return new SimpleTestResult(true, Name + ": Okay"); - } - - public static void Main( - string[] args) - { - ITest test = new TigerDigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } + public static void Main( + string[] args) + { + RunTest(new TigerDigestTest()); + } [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); + public void TestFunction() + { + string resultText = Perform().ToString(); - Assert.AreEqual(Name + ": Okay", resultText); - } + Assert.AreEqual(Name + ": Okay", resultText); + } } } diff --git a/crypto/test/src/crypto/test/WhirlpoolDigestTest.cs b/crypto/test/src/crypto/test/WhirlpoolDigestTest.cs index 865408f6a..eff9b1c2e 100644 --- a/crypto/test/src/crypto/test/WhirlpoolDigestTest.cs +++ b/crypto/test/src/crypto/test/WhirlpoolDigestTest.cs @@ -5,135 +5,107 @@ using NUnit.Framework; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Encodings; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { + /** * ISO vector test for Whirlpool - * + * */ [TestFixture] public class WhirlpoolDigestTest - : SimpleTest + : DigestTest { - private static string[][] _isoVectors = + private static string[] messages = { - new string[] - { - "", - "19FA61D75522A4669B44E39C1D2E1726C530232130D407F89AFEE0964997F7A73E83BE698B288FEBCF88E3E03C4F0757EA8964E59B63D93708B138CC42A66EB3" - }, - new string[] - { - "a", - "8ACA2602792AEC6F11A67206531FB7D7F0DFF59413145E6973C45001D0087B42D11BC645413AEFF63A42391A39145A591A92200D560195E53B478584FDAE231A" - }, - new string[] - { - "abc", - "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5" - }, - new string[] - { - "message digest", - "378C84A4126E2DC6E56DCC7458377AAC838D00032230F53CE1F5700C0FFB4D3B8421557659EF55C106B4B52AC5A4AAA692ED920052838F3362E86DBD37A8903E" - }, - new string[] - { - "abcdefghijklmnopqrstuvwxyz", - "F1D754662636FFE92C82EBB9212A484A8D38631EAD4238F5442EE13B8054E41B08BF2A9251C30B6A0B8AAE86177AB4A6F68F673E7207865D5D9819A3DBA4EB3B" - }, - new string[] - { - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "DC37E008CF9EE69BF11F00ED9ABA26901DD7C28CDEC066CC6AF42E40F82F3A1E08EBA26629129D8FB7CB57211B9281A65517CC879D7B962142C65F5A7AF01467" - }, - new string[] - { - "12345678901234567890123456789012345678901234567890123456789012345678901234567890", - "466EF18BABB0154D25B9D38A6414F5C08784372BCCB204D6549C4AFADB6014294D5BD8DF2A6C44E538CD047B2681A51A2C60481E88C5A20B2C2A80CF3A9A083B" - }, - new string[] - { - "abcdbcdecdefdefgefghfghighijhijk", - "2A987EA40F917061F5D6F0A0E4644F488A7A5A52DEEE656207C562F988E95C6916BDC8031BC5BE1B7B947639FE050B56939BAAA0ADFF9AE6745B7B181C3BE3FD" - } + "", + "a", + "abc", + "message digest", + "abcdefghijklmnopqrstuvwxyz", + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "12345678901234567890123456789012345678901234567890123456789012345678901234567890", + "abcdbcdecdefdefgefghfghighijhijk" + }; + + private static string[] digests = + { + "19FA61D75522A4669B44E39C1D2E1726C530232130D407F89AFEE0964997F7A73E83BE698B288FEBCF88E3E03C4F0757EA8964E59B63D93708B138CC42A66EB3", + "8ACA2602792AEC6F11A67206531FB7D7F0DFF59413145E6973C45001D0087B42D11BC645413AEFF63A42391A39145A591A92200D560195E53B478584FDAE231A", + "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5", + "378C84A4126E2DC6E56DCC7458377AAC838D00032230F53CE1F5700C0FFB4D3B8421557659EF55C106B4B52AC5A4AAA692ED920052838F3362E86DBD37A8903E", + "F1D754662636FFE92C82EBB9212A484A8D38631EAD4238F5442EE13B8054E41B08BF2A9251C30B6A0B8AAE86177AB4A6F68F673E7207865D5D9819A3DBA4EB3B", + "DC37E008CF9EE69BF11F00ED9ABA26901DD7C28CDEC066CC6AF42E40F82F3A1E08EBA26629129D8FB7CB57211B9281A65517CC879D7B962142C65F5A7AF01467", + "466EF18BABB0154D25B9D38A6414F5C08784372BCCB204D6549C4AFADB6014294D5BD8DF2A6C44E538CD047B2681A51A2C60481E88C5A20B2C2A80CF3A9A083B", + "2A987EA40F917061F5D6F0A0E4644F488A7A5A52DEEE656207C562F988E95C6916BDC8031BC5BE1B7B947639FE050B56939BAAA0ADFF9AE6745B7B181C3BE3FD" }; private static string _millionAResultVector = "0C99005BEB57EFF50A7CF005560DDF5D29057FD86B20BFD62DECA0F1CCEA4AF51FC15490EDDC47AF32BB2B66C34FF9AD8C6008AD677F77126953B226E4ED8B01"; private static string _thirtyOneZeros = "3E3F188F8FEBBEB17A933FEAF7FE53A4858D80C915AD6A1418F0318E68D49B4E459223CD414E0FBC8A57578FD755D86E827ABEF4070FC1503E25D99E382F72BA"; - public override string Name + internal WhirlpoolDigestTest() + : base(new WhirlpoolDigest(), messages, digests) { - get - { - return "Whirlpool"; - } } public override void PerformTest() { - for (int i = 0; i < _isoVectors.Length; i++) - { - performStandardVectorTest("ISO vector test ["+i+"]", _isoVectors[i][0], - _isoVectors[i][1]); - } + base.PerformTest(); byte[] thirtyOneZeros = new byte[31]; - performStandardVectorTest("31 zeroes test", - thirtyOneZeros, _thirtyOneZeros); + performStandardVectorTest("31 zeroes test", + thirtyOneZeros, _thirtyOneZeros); byte[] millionAInByteArray = new byte[1000000]; -// Arrays.fill(millionAInByteArray, (byte)'a'); - for (int ai = 0; ai < millionAInByteArray.Length; ++ai) - { - millionAInByteArray[ai] = (byte) 'a'; - } + Arrays.Fill(millionAInByteArray, (byte)'a'); - performStandardVectorTest("Million 'a' test", - millionAInByteArray, _millionAResultVector); + performStandardVectorTest("Million 'a' test", + millionAInByteArray, _millionAResultVector); } private void performStandardVectorTest(string testTitle, byte[] inputBytes, - string resultsAsHex) + string resultsAsHex) { - doPerformTest(testTitle, inputBytes, resultsAsHex); + doPerformTest(testTitle, inputBytes, resultsAsHex); } private void doPerformTest(string testTitle, byte[] inputBytes, string resultsAsHex) { string resStr = createHexOutputFromDigest(inputBytes); - if (resultsAsHex != resStr.ToUpper()) + if (!resultsAsHex.Equals(resStr.ToUpper())) { Fail(testTitle, resultsAsHex, resStr); } } - private void performStandardVectorTest(string testTitle, string inputBytesAsString, - string resultsAsHex) - { - doPerformTest(testTitle, Encoding.ASCII.GetBytes(inputBytesAsString), resultsAsHex); - } - private string createHexOutputFromDigest(byte[] digestBytes) { + string resStr; IDigest digest = new WhirlpoolDigest(); byte[] resBuf = new byte[digest.GetDigestSize()]; digest.BlockUpdate(digestBytes, 0, digestBytes.Length); digest.DoFinal(resBuf, 0); - return Hex.ToHexString(resBuf); + resStr = Hex.ToHexString(resBuf); + return resStr; + } + + protected override IDigest CloneDigest(IDigest digest) + { + return new WhirlpoolDigest((WhirlpoolDigest)digest); } public static void Main( string[] args) { - WhirlpoolDigestTest test = new WhirlpoolDigestTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); + RunTest(new WhirlpoolDigestTest()); } [Test] |