@Test public void testMD4() { MAC hmac = HMAC.md4(ascii(EMPTY_STRING)); assertArrayEquals(hex("c8d444e3153b538850e7850fa84bb247"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.md4(ascii("key")); assertArrayEquals(hex("8d3366c440a9c65124ab0b5f4ca27338"), hmac.digest(ascii(PANGRAM))); }
@Test public void testMD5() { MAC hmac = HMAC.md5(ascii(EMPTY_STRING)); assertArrayEquals(hex("74e6f7298a9c2d168935f58c001bad88"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.md5(ascii("key")); assertArrayEquals(hex("80070713463e7749b90c2dc24911e275"), hmac.digest(ascii(PANGRAM))); }
/** * Creates and returns a {@link MAC} instance corresponding to the given algorithm, initialized * with the provided secret key. * * @param algorithm the MAC algorithm. * @param key the secret key. * @return the created {@link MAC} instance. * @throws NullPointerException if {@code algorithm} is {@code null}. * @throws IllegalArgumentException if the given algorithm is unknown. */ static MAC newMAC(Algorithm<MAC> algorithm, byte[] key) { Parameters.checkNotNull(algorithm); MAC mac; if (algorithm == Algorithm.HMAC_MD2) { mac = HMAC.md2(key); } else if (algorithm == Algorithm.HMAC_MD4) { mac = HMAC.md4(key); } else if (algorithm == Algorithm.HMAC_MD5) { mac = HMAC.md5(key); } else if (algorithm == Algorithm.HMAC_SHA1) { mac = HMAC.sha1(key); } else if (algorithm == Algorithm.HMAC_SHA256) { mac = HMAC.sha256(key); } else if (algorithm == Algorithm.HMAC_SHA512) { mac = HMAC.sha512(key); } else if (algorithm == Algorithm.HMAC_KECCAK224) { mac = HMAC.keccak224(key); } else if (algorithm == Algorithm.HMAC_KECCAK256) { mac = HMAC.keccak256(key); } else if (algorithm == Algorithm.HMAC_KECCAK384) { mac = HMAC.keccak384(key); } else if (algorithm == Algorithm.HMAC_KECCAK512) { mac = HMAC.keccak512(key); } else { throw new IllegalArgumentException("Unknown algorithm"); } return mac; }
@Test public void testMD2() { MAC hmac = HMAC.md2(ascii(EMPTY_STRING)); assertArrayEquals(hex("6f6e031223b36cd2a997787a03d16bf5"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.md2(ascii("key")); assertArrayEquals(hex("13758b9534bfb38d850457814613b0c1"), hmac.digest(ascii(PANGRAM))); }
@Test public void testSHA1() { MAC hmac = HMAC.sha1(ascii(EMPTY_STRING)); assertArrayEquals( hex("fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.sha1(ascii("key")); assertArrayEquals(hex("de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"), hmac.digest(ascii(PANGRAM))); }
@Test public void testSHA256() { MAC hmac = HMAC.sha256(ascii(EMPTY_STRING)); assertArrayEquals( hex("b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6" + "c712144292c5ad"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.sha256(ascii("key")); assertArrayEquals( hex("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997" + "479dbc2d1a3cd8"), hmac.digest(ascii(PANGRAM))); }
@Test public void testKeccak256() { MAC hmac = HMAC.keccak256(ascii(EMPTY_STRING)); assertArrayEquals( hex("042186ec4e98680a0866091d6fb89b60871134b44327f8f467" + "c14e9841d3e97b"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.keccak256(ascii("key")); assertArrayEquals( hex("74547bc8c8e1ef02aec834ca60ff24cc316d4c2244a360fe17" + "448cb53410bed4"), hmac.digest(ascii(PANGRAM))); }
@Test public void testKeccak224() { MAC hmac = HMAC.keccak224(ascii(EMPTY_STRING)); assertArrayEquals( hex("5b71601b23a34517acd63847e99e461baf705e71884b986b4c" + "f64e19"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.keccak224(ascii("key")); assertArrayEquals( hex("763e70a1ec866fbc1c6e6c398cd6e2383e2ad3aecbb3d6150f" + "1e56fd"), hmac.digest(ascii(PANGRAM))); }
@Test public void testSHA512() { MAC hmac = HMAC.sha512(ascii(EMPTY_STRING)); assertArrayEquals( hex( "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b" + "70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb4" + "9ad7381eb067b338fd7b0cb22247225d47"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.sha512(ascii("key")); assertArrayEquals( hex( "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe8" + "6653c73dd248fb82f948a549f7b791a5b41915ee4d1e" + "c3935357e4e2317250d0372afa2ebeeb3a"), hmac.digest(ascii(PANGRAM))); }
@Test public void testKeccak512() { MAC hmac = HMAC.keccak512(ascii(EMPTY_STRING)); assertArrayEquals( hex( "a857c9f1cd9cb25c2f24a933618abfc724d4eaebf74099dddc" + "5e0a1c7ab8de3865faa1fb039419de768e962bdc81a6" + "d85b9809d6ab84f8cc984f5d6b90f23c15"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.keccak512(ascii("key")); assertArrayEquals( hex( "22fb03b3391bc0adfc73c18e0919d9f142390e81d6cc268971" + "6ac53ab75458a718059d58cfbb23c6a416c32b8afa84" + "a9a7a9d852312a743bef0a55148e5a1b8a"), hmac.digest(ascii(PANGRAM))); }
@Test public void testKeccak384() { MAC hmac = HMAC.keccak384(ascii(EMPTY_STRING)); assertArrayEquals( hex( "09e18efb747979fcbc89849ff5c388a763c5fed1836a2850b7" + "9598eaa3a5cf014d57e337923e69c6f4a2dade032b02" + "40"), hmac.digest(ascii(EMPTY_STRING))); hmac = HMAC.keccak384(ascii("key")); assertArrayEquals( hex( "73acb07b5b1db5431758262b55e5923d362de4492229a74203" + "02c80d4348ca1b11ecea06fb1c232f9b832aadca8cd2" + "89"), hmac.digest(ascii(PANGRAM))); }
/** Generates an SHA1 hash given input and a key. */ public static String hash(String plainText, String hashKey) { // temporary output storage byte[] digestData = {}; // generate hash try { HMACKey key = new HMACKey(hashKey.getBytes()); HMAC hMac = new HMAC(key, new SHA1Digest()); MACOutputStream macStream = new MACOutputStream(hMac, null); macStream.write(plainText.getBytes()); hMac.getMAC(digestData, 0); } catch (CryptoException e) { System.out.println("Crypt Exception: " + e); return "BAD Crypto"; } catch (IOException e) { System.out.println("IOException: " + e); return "BAD IO"; } // return hash return new String(digestData); }