public void testEmptyInputs() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(MD5_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString()); }
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 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 testEmptyInputs_mixedAlgorithms() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(SHA1_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString()); }
public void testKnownInputs_mixedAlgorithms() throws Exception { String knownOutput = "9753980fe94daa8ecaa82216519393a9"; String input = "The quick brown fox jumps over the lazy dog"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(SHA1_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(SHA1_KEY).hashString(input, UTF_8).toString()); assertEquals( knownOutput, Hashing.hmacMd5(SHA1_KEY).hashBytes(input.getBytes(UTF_8)).toString()); }
private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) { assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data)); }
public void testCustomKey() throws Exception { SecretKey customKey = new SecretKey() { @Override public String getAlgorithm() { return "HmacMD5"; } @Override public byte[] getEncoded() { return new byte[8]; } @Override public String getFormat() { return "RAW"; } }; assertEquals( "ad262969c53bc16032f160081c4a07a0", Hashing.hmacMd5(customKey) .hashString("The quick brown fox jumps over the lazy dog", UTF_8) .toString()); }
public void testMultipleUpdates() throws Exception { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(SHA1_KEY); mac.update("hello".getBytes(UTF_8)); mac.update("world".getBytes(UTF_8)); assertEquals( HashCode.fromBytes(mac.doFinal()), Hashing.hmacSha1(SHA1_KEY) .newHasher() .putString("hello", UTF_8) .putString("world", UTF_8) .hash()); }
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 testToString() { byte[] keyData = "secret key".getBytes(UTF_8); assertEquals( "Hashing.hmacMd5(Key[algorithm=HmacMD5, format=RAW])", Hashing.hmacMd5(MD5_KEY).toString()); assertEquals( "Hashing.hmacMd5(Key[algorithm=HmacMD5, format=RAW])", Hashing.hmacMd5(keyData).toString()); assertEquals( "Hashing.hmacSha1(Key[algorithm=HmacSHA1, format=RAW])", Hashing.hmacSha1(SHA1_KEY).toString()); assertEquals( "Hashing.hmacSha1(Key[algorithm=HmacSHA1, format=RAW])", Hashing.hmacSha1(keyData).toString()); assertEquals( "Hashing.hmacSha256(Key[algorithm=HmacSHA256, format=RAW])", Hashing.hmacSha256(SHA256_KEY).toString()); assertEquals( "Hashing.hmacSha256(Key[algorithm=HmacSHA256, format=RAW])", Hashing.hmacSha256(keyData).toString()); assertEquals( "Hashing.hmacSha512(Key[algorithm=HmacSHA512, format=RAW])", Hashing.hmacSha512(SHA512_KEY).toString()); assertEquals( "Hashing.hmacSha512(Key[algorithm=HmacSHA512, format=RAW])", Hashing.hmacSha512(keyData).toString()); }
public void testMultipleUpdatesDoFinal() throws Exception { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(SHA1_KEY); mac.update("hello".getBytes(UTF_8)); mac.update("world".getBytes(UTF_8)); assertEquals( HashCode.fromBytes(mac.doFinal("!!!".getBytes(UTF_8))), Hashing.hmacSha1(SHA1_KEY) .newHasher() .putString("hello", UTF_8) .putString("world", UTF_8) .putString("!!!", UTF_8) .hash()); }
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) { } }
public void testKnownInputs_mixedAlgorithms() throws Exception { String knownOutput = "9753980fe94daa8ecaa82216519393a9"; String input = "The quick brown fox jumps over the lazy dog"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(SHA1_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(SHA1_KEY).hashString(input, UTF_8).toString()); assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).hashBytes(input.getBytes(UTF_8)).toString()); }
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 testEmptyInputs() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(MD5_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString()); }
public void testEmptyInputs_mixedAlgorithms() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(SHA1_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString()); }
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()); }
public void testMultipleUpdates() throws Exception { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(SHA1_KEY); mac.update("hello".getBytes(UTF_8)); mac.update("world".getBytes(UTF_8)); assertEquals( HashCode.fromBytes(mac.doFinal()), Hashing.hmacSha1(SHA1_KEY).newHasher() .putString("hello", UTF_8) .putString("world", UTF_8) .hash()); }
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 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) { } }