1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Simple fast portable secure hashing library */
3 /* Copyright(c) 2016-2019 LoRd_MuldeR <mulder2@gmx.de> */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
6 /* and associated documentation files (the "Software"), to deal in the Software without */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish, */
8 /* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
16 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 /* ---------------------------------------------------------------------------------------------- */
23 using Microsoft.VisualStudio.TestTools.UnitTesting;
24 using System.Diagnostics;
26 namespace com.muldersoft.mhash384.test
29 public class MHash384Test
31 private static bool m_success;
34 public static void Initialize(TestContext testContext)
36 Trace.WriteLine(String.Format("MHash-384 v{0:D}.{1:D2}-{2:D}, Microsoft.NET v{3} [{4:D}-Bit]\n", MHash384.VERSION_MAJOR, MHash384.VERSION_MINOR, MHash384.VERSION_PATCH, System.Environment.Version, System.Environment.Is64BitProcess ? 64 : 32));
41 public static void Cleanup()
45 Trace.WriteLine("\nSelf-test completed successfully :-)");
49 Trace.WriteLine("\nError: Self-test has failed! :-(");
53 //=======================================================================
55 //=======================================================================
58 public void TestVector_01()
63 "D629CB37F76156621E09C93B8D1029C5F0F7B778DE5BD9578230673FE7FA4C45A2308AB4E9D99AA0EA194CBF6230C202"
68 public void TestVector_02()
73 "1D7B1593D7AA4DD7DC8DA6D6BD591D8B507CA514E1C7EC37F43E78DD6B83253C355403F61009582890480C4CD527F405"
78 public void TestVector_03()
82 1, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
83 "9B98CE4C3F4EA9018E1ED3034717E3E5D8DA20AB375BAFE372D07780FFDEFDA32BA3C822155E2883E333AFCAAEAFA614"
88 public void TestVector_04()
92 1, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
93 "45416D1FFD44D3940C5AF5B425B5DA07FD8C06BAB7C58CD13389BC284650ED6090A9A1952C29FEB1B2C6AFBF5F996108"
98 public void TestVector_05()
102 0x186A0, "aaaaaaaaaa",
103 "20774C570AE2E6A49A01DAE948672AE94D465D9167130FE87BF24148649CF121E8A50759507BF97134F0D28CEE91DCA2"
108 public void TestVector_06()
112 0x1000000, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
113 "C18843D4DD9E0479D38A55E8C75E2201F0E93B6EF1F916EBAF3C4074D188945C7B9657F45269A1DAAB4A8FB6F63D0BEE"
118 public void TestVector_07()
122 1, "The quick brown fox jumps over the lazy dog",
123 "E2901EDB76AA83F2D52FEA9A34EB2BC05EDFA9571C812620245BBEBA95FA2D82CBB5BAD93EB64D4B72B46635D565400B"
128 public void TestVector_08()
132 1, "The quick brown fox jumps over the lazy cog",
133 "8B81CD9C1DEE8BCC642F8B1EA982CE79D15F50E66939FF34294E63D93AE3C610F9464FEFEF30297409B62231F028ABC0"
138 public void TestVector_09()
142 1, "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern",
143 "A545E6319F6ACE0CEF62BCAAF5D488858751B96C2A488D5BE6EC63D50006C9D54DAD66168488F03B4A868D3BA610CCF8"
148 public void TestVector_10()
152 1, "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern",
153 "2BECB89DD55FC9F2F3384DF61D6240DDA54FC275D186A45A883C03AA873DDAF4FAD4744212B92D26054B92852DEA5F1D"
158 public void TestVector_11()
162 1, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
163 "6283CE91DA8AF5091C7B06E1352DC947B9E3B89614807C04C43DA322321FBADAD3CCCABECB5C1B1B265432A6039121E1"
168 public void TestVector_12()
172 1, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamc0 laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
173 "5DCB7C4B27BA841688FB6A860E97A45256917533F63448CF666781EA4B5FAB20E1C746C8821099A915222E0D77624098"
177 //=======================================================================
179 //=======================================================================
181 private static void RunTest(uint repetitions, string test, string expected)
183 string digest = GetDigest(repetitions, test);
184 bool matches = digest.Equals(expected, StringComparison.OrdinalIgnoreCase);
185 m_success &= matches;
186 Trace.WriteLine(String.Format("{0} - {1}", digest, matches ? "OK" : "Error!"));
187 Assert.IsTrue(matches);
190 private static string GetDigest(uint repetitions, string test)
192 MHash384 hash = new MHash384();
193 for(uint i = 0; i < repetitions; ++i)
197 return ToHexString(hash.GetDigest());
200 private static string ToHexString(byte[] bytes)
202 StringBuilder sb = new StringBuilder(bytes.Length * 2);
203 foreach (byte b in bytes)
205 sb.AppendFormat("{0:X2}", b);
207 return sb.ToString();