public void testKnownInputs() throws Exception { String knownOutput = "9753980fe94daa8ecaa82216519393a9"; String input = "The quick brown fox jumps over the lazy dog"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(MD5_KEY); mac.update(input.getBytes(UTF_8)); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString()); assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashString(input, UTF_8).toString()); assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashBytes(input.getBytes(UTF_8)).toString()); }
private static void checkSha1(String expected, byte[] key, byte[] data) { checkHmac(expected, Hashing.hmacSha1(key), data); }
public void testRfc2202_hmacMd5_case1() { byte[] key = fillByteArray(16, 0x0b); String data = "Hi There"; checkMd5("9294727a3638bb1c13f48ef8158bfc9d", key, data); }
public void testRfc2202_hmacSha1_case6() { byte[] key = fillByteArray(80, 0xaa); String data = "Test Using Larger Than Block-Size Key - Hash Key First"; checkSha1("aa4ae5e15272d00e95705637ce8a3b55ed402112", key, data); }
public void testHashTwice() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals( "9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.hash(); fail(); } catch (IllegalStateException expected) { } }
public void testRfc2202_hmacMd5_case2() { byte[] key = "Jefe".getBytes(UTF_8); String data = "what do ya want for nothing?"; checkMd5("750c783e6ab0b503eaa86e310a5db738", key, data); }
public void testBadKey_emptyKey() throws Exception { SecretKey badKey = new SecretKey() { @Override public String getAlgorithm() { return "HmacMD5"; } @Override public byte[] getEncoded() { return null; } @Override public String getFormat() { return "RAW"; } }; try { Hashing.hmacMd5(badKey); fail(); } catch (IllegalArgumentException expected) { } catch (NullPointerException toleratedOnAndroid) { // TODO(cpovirk): In an ideal world, we'd check here that we're running on Android. } }
public void testRfc2202_hmacSha1_case2() { byte[] key = "Jefe".getBytes(UTF_8); String data = "what do ya want for nothing?"; checkSha1("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", key, data); }
public void testHashing() throws Exception { for (String stringToTest : INPUTS) { for (Table.Cell<String, SecretKey, HashFunction> cell : ALGORITHMS.cellSet()) { String algorithm = cell.getRowKey(); SecretKey key = cell.getColumnKey(); HashFunction hashFunc = cell.getValue(); assertMacHashing(HashTestUtils.ascii(stringToTest), algorithm, key, hashFunc); } } }
public void testRfc2202_hmacSha1_case7() { byte[] key = fillByteArray(80, 0xaa); String data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; checkSha1("e8e99d0f45237d786d6bbaa7965c7808bbff1a91", key, data); }
public void testPutAfterHash() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals( "9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.putInt(42); fail(); } catch (IllegalStateException expected) { } }
private static void checkMd5(String expected, byte[] key, String data) { checkMd5(expected, key, data.getBytes(UTF_8)); }
@AndroidIncompatible // sun.security public void testNoProviders() { ProviderList providers = Providers.getProviderList(); Providers.setProviderList(ProviderList.newList()); try { Hashing.hmacMd5(MD5_KEY); fail("expected ISE"); } catch (IllegalStateException expected) { } finally { Providers.setProviderList(providers); } }
private static void checkSha1(String expected, byte[] key, String data) { checkSha1(expected, key, data.getBytes(UTF_8)); }
public void testHashing() throws Exception { for (String stringToTest : INPUTS) { for (Table.Cell<String, SecretKey, HashFunction> cell : ALGORITHMS.cellSet()) { String algorithm = cell.getRowKey(); SecretKey key = cell.getColumnKey(); HashFunction hashFunc = cell.getValue(); assertMacHashing(HashTestUtils.ascii(stringToTest), algorithm, key, hashFunc); } } }
public void testRfc2202_hmacSha1_case5() { byte[] key = fillByteArray(20, 0x0c); String data = "Test With Truncation"; checkSha1("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04", key, data); }
public void testRfc2202_hmacMd5_case6() { byte[] key = fillByteArray(80, 0xaa); String data = "Test Using Larger Than Block-Size Key - Hash Key First"; checkMd5("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd", key, data); }
private static void assertMacHashing( byte[] input, String algorithm, SecretKey key, HashFunction hashFunc) throws Exception { Mac mac = Mac.getInstance(algorithm); mac.init(key); mac.update(input); assertEquals(HashCode.fromBytes(mac.doFinal()), hashFunc.hashBytes(input)); assertEquals(HashCode.fromBytes(mac.doFinal(input)), hashFunc.hashBytes(input)); }
public void testHashTwice() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals("9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.hash(); fail(); } catch (IllegalStateException expected) { } }
public void testRfc2202_hmacMd5_case2() { byte[] key = "Jefe".getBytes(UTF_8); String data = "what do ya want for nothing?"; checkMd5("750c783e6ab0b503eaa86e310a5db738", key, data); }