private static void testKat(Digest dig, byte[] buf, byte[] exp) { /* * First test the hashing itself. */ byte[] out = dig.digest(buf); assertEquals(out, exp); /* * Now the update() API; this also exercises auto-reset. */ for (int i = 0; i < buf.length; i ++) dig.update(buf[i]); assertEquals(dig.digest(), exp); /* * The cloning API. */ int blen = buf.length; dig.update(buf, 0, blen / 2); Digest dig2 = dig.copy(); dig.update(buf, blen / 2, blen - (blen / 2)); assertEquals(dig.digest(), exp); dig2.update(buf, blen / 2, blen - (blen / 2)); assertEquals(dig2.digest(), exp); }
/** * Test MD2 implementation. */ private static void testMD2() { Digest dig = new MD2(); testKat(dig, "", "8350e5a3e24c153df2275c9f80692773"); testKat(dig, "a", "32ec01ec4a6dac72c0ab96fb34c0b5d1"); testKat(dig, "abc", "da853b0d3f88d99b30283a69e6ded6bb"); testKat(dig, "message digest", "ab4f496bfb2a530b219ff33031fe06b0"); testKat(dig, "abcdefghijklmnopqrstuvwxyz", "4e8ddff3650292ab5a4108c3aa47940b"); testKat(dig, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" + "vwxyz0123456789", "da33def2a42df13975352846c30338cd"); testKat(dig, "1234567890123456789012345678901234567890123456789" + "0123456789012345678901234567890", "d5976f79d83d3a0dc9806c3c66f3efd8"); testKatMillionA(dig, "8c0a09ff1216ecaf95c8130953c62efd"); reportSuccess("MD2"); }
/** * Test SHA-384 implementation. */ private static void testSHA384() { Digest dig = new SHA384(); testKat(dig, "abc", "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163" + "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"); testKat(dig, "abcdefghbcdefghicdefghijdefghijkefghijklfghij" + "klmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnop" + "qrsmnopqrstnopqrstu", "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2" + "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"); testKatMillionA(dig, "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852" + "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"); reportSuccess("SHA-384"); }
/** * Test SHA-512 implementation. */ private static void testSHA512() { Digest dig = new SHA512(); testKat(dig, "abc", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); testKat(dig, "abcdefghbcdefghicdefghijdefghijkefghijklfghij" + "klmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnop" + "qrsmnopqrstnopqrstu", "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); testKatMillionA(dig, "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" + "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"); reportSuccess("SHA-512"); }
/** * Test PANAMA implementation. */ private static void testPANAMA() { Digest dig = new PANAMA(); testKat(dig, "", "aa0cc954d757d7ac7779ca3342334ca471abd47d5952ac91ed837ecd5b16922b"); testKat(dig, "T", "049d698307d8541f22870dfa0a551099d3d02bc6d57c610a06a4585ed8d35ff8"); testKat(dig, "The quick brown fox jumps over the lazy dog", "5f5ca355b90ac622b0aa7e654ef5f27e9e75111415b48b8afe3add1c6b89cba1"); testKatMillionA(dig, "af9c66fb6058e2232a5dfba063ee14b0f86f0e334e165812559435464dd9bb60"); reportSuccess("PANAMA"); }
private static void testKatMillionA(Digest dig, String ref) { byte[] buf = new byte[1000]; for (int i = 0; i < 1000; i ++) buf[i] = 'a'; for (int i = 0; i < 1000; i ++) dig.update(buf); assertEquals(dig.digest(), strtobin(ref)); }
private static void testCollision(Digest dig, String s1, String s2) { byte[] msg1 = strtobin(s1); byte[] msg2 = strtobin(s2); assertNotEquals(msg1, msg2); assertEquals(dig.digest(msg1), dig.digest(msg2)); }
/** * Test MD4 implementation. */ private static void testMD4() { Digest dig = new MD4(); testKat(dig, "", "31d6cfe0d16ae931b73c59d7e0c089c0"); testKat(dig, "a", "bde52cb31de33e46245e05fbdbd6fb24"); testKat(dig, "abc", "a448017aaf21d8525fc10ae87aa6729d"); testKat(dig, "message digest", "d9130a8164549fe818874806e1c7014b"); testKat(dig, "abcdefghijklmnopqrstuvwxyz", "d79e1c308aa5bbcdeea8ed63df412da9"); testKat(dig, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu" + "vwxyz0123456789", "043f8582f241db351ce627e153e7f0e4"); testKat(dig, "1234567890123456789012345678901234567890123456789" + "0123456789012345678901234567890", "e33b4ddc9c38f2199c3e7b164fcc0536"); testKatMillionA(dig, "bbce80cc6bb65e5c6745e30d4eeca9a4"); testCollision(dig, "839c7a4d7a92cb5678a5d5b9eea5a7573c8a74deb366c3dc20" + "a083b69f5d2a3bb3719dc69891e9f95e809fd7e8b23ba631" + "8edd45e51fe39708bf9427e9c3e8b9", "839c7a4d7a92cbd678a5d529eea5a7573c8a74deb366c3dc20" + "a083b69f5d2a3bb3719dc69891e9f95e809fd7e8b23ba631" + "8edc45e51fe39708bf9427e9c3e8b9"); testCollision(dig, "839c7a4d7a92cb5678a5d5b9eea5a7573c8a74deb366c3dc20" + "a083b69f5d2a3bb3719dc69891e9f95e809fd7e8b23ba631" + "8edd45e51fe39740c213f769cfb8a7", "839c7a4d7a92cbd678a5d529eea5a7573c8a74deb366c3dc20" + "a083b69f5d2a3bb3719dc69891e9f95e809fd7e8b23ba631" + "8edc45e51fe39740c213f769cfb8a7"); reportSuccess("MD4"); }
/** * Test SHA-0 implementation. */ private static void testSHA0() { Digest dig = new SHA0(); testKat(dig, "abc", "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880"); testKat(dig, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlm" + "nomnopnopq", "d2516ee1acfa5baf33dfc1c471e438449ef134c8"); testKatMillionA(dig, "3232affa48628a26653b5aaa44541fd90d690603"); testCollision(dig, "a766a602b65cffe773bcf25826b322b3d01b1a972684ef533e" + "3b4b7f53fe376224c08e47e959b2bc3b519880b928656824" + "7d110f70f5c5e2b4590ca3f55f52feeffd4c8fe68de83532" + "9e603cc51e7f02545410d1671d108df5a4000dcf20a43949" + "49d72cd14fbb0345cf3a295dcda89f998f87552c9a58b1bd" + "c384835e477185f96e68bebb0025d2d2b69edf21724198f6" + "88b41deb9b4913fbe696b5457ab39921e1d7591f89de8457" + "e8613c6c9e3b242879d4d8783b2d9ca9935ea526a729c06e" + "dfc50137e69330be976012cc5dfe1c14c4c68bd1db3ecb24" + "438a59a09b5db435563e0d8bdf572f77b53065cef31f32dc" + "9dbaa04146261e9994bd5cd0758e3d", "a766a602b65cffe773bcf25826b322b1d01b1ad72684ef51be" + "3b4b7fd3fe3762a4c08e45e959b2fc3b51988039286528a4" + "7d110d70f5c5e034590ce3755f52fc6ffd4c8d668de87532" + "9e603e451e7f02d45410d1e71d108df5a4000dcf20a43949" + "49d72cd14fbb0145cf3a695dcda89d198f8755ac9a58b13d" + "c384815e4771c5796e68febb0025d052b69edda17241d876" + "88b41f6b9b49117be696f5c57ab399a1e1d7199f89de8657" + "e8613cec9e3b26a879d498783b2d9e29935ea7a6a729806e" + "dfc50337e693303e9760104c5dfe5c14c4c68951db3ecba4" + "438a59209b5db435563e0d8bdf572f77b53065cef31f30dc" + "9dbae04146261c1994bd5c50758e3d"); reportSuccess("SHA-0"); }