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()); }
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 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()); }
public void testHash() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); String init = "d41d8cd98f00b204e9800998ecf8427e"; assertEquals(init, Hashing.md5().newHasher().hash().toString()); String asciiHash = "e5df5a39f2b8cb71b24e1d8038f93131"; assertEquals(asciiHash, Files.hash(asciiFile, Hashing.md5()).toString()); String i18nHash = "7fa826962ce2079c8334cd4ebf33aea4"; assertEquals(i18nHash, Files.hash(i18nFile, Hashing.md5()).toString()); }
public void testRoundTrip() { for (ExpectedHashCode expected : expectedHashCodes) { String string = HashCode.fromBytes(expected.bytes).toString(); assertEquals(expected.toString, string); assertEquals( expected.toString, HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(string)).toString()); } }
public void testHash() throws IOException { ByteSource byteSource = new TestByteSource("hamburger\n".getBytes(Charsets.US_ASCII)); // Pasted this expected string from `echo hamburger | md5sum` assertEquals("cfa0c5002275c90508338a5cdb2a9781", byteSource.hash(Hashing.md5()).toString()); }
public void testFromStringFailsWithUpperCaseString() { String string = Hashing.sha1().hashString("foo", Charsets.US_ASCII).toString().toUpperCase(); try { HashCode.fromString(string); fail(); } catch (IllegalArgumentException expected) { } }
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 testRoundTripHashCodeUsingBaseEncoding() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(hash1.toString())); assertEquals(hash1, hash2); }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
public void testHashTwice() { Hasher sha1 = Hashing.sha1().newHasher(); assertEquals( "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) .hash() .toString()); try { sha1.hash(); fail(); } catch (IllegalStateException expected) { } }
public void testToString() { byte[] data = new byte[] {127, -128, 5, -1, 14}; assertEquals("7f8005ff0e", HashCode.fromBytes(data).toString()); assertEquals("7f8005ff0e", base16().lowerCase().encode(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) { } }
public void testKnownUtf8Hashing() { for (Cell<HashFunction, String, String> cell : KNOWN_HASHES.cellSet()) { HashFunction func = cell.getRowKey(); String input = cell.getColumnKey(); String expected = cell.getValue(); assertEquals( String.format(Locale.ROOT, "Known hash for hash(%s, UTF_8) failed", input), expected, func.hashString(input, UTF_8).toString()); } }
public void testPutAfterHash() { Hasher sha1 = Hashing.sha1().newHasher(); assertEquals( "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) .hash() .toString()); try { sha1.putInt(42); fail(); } catch (IllegalStateException expected) { } }
public void testFromBytesNoCopy_noCopyOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytesNoCopy(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testFromBytes_copyOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytes(bytes); int expectedInt = 0x0000abcd; String expectedToString = "cdab0000"; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); }
public void testGetBytesInternal_noCloneOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytes(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); hashCode.getBytesInternal()[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testKnownValues() { assertHash(0, 0x629942693e10f867L, 0x92db0b82baeb5347L, "hell"); assertHash(1, 0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello"); assertHash(2, 0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello "); assertHash(3, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L, "hello w"); assertHash(4, 0x79f6305a386c572cL, 0x46305aed3483b94eL, "hello wo"); assertHash(5, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL, "hello wor"); assertHash( 0, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L, "The quick brown fox jumps over the lazy dog"); assertHash( 0, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL, "The quick brown fox jumps over the lazy cog"); // Known output from Python smhasher HashCode foxHash = murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8); assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.toString()); }