public void testCollisionsDueToIncorrectSignExtension() { byte[] col1 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0x80}; byte[] col2 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0x81}; byte[] col3 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte) 0xff}; ImmutableSet<HashCode> hashCodes = ImmutableSet.of( SIP_WITH_KEY.hashBytes(col1), SIP_WITH_KEY.hashBytes(col2), SIP_WITH_KEY.hashBytes(col3)); assertEquals(3, hashCodes.size()); }
static void assertHashByteBufferExhaustsBuffer(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes); HashCode unused = hashFunction.hashBytes(buffer); assertFalse(buffer.hasRemaining()); }
@Override public byte[] hash(byte[] input, int seed) { return murmur3_32(seed).hashBytes(input).asBytes(); } };
static void assertHashByteBufferPreservesByteOrder(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); assertEquals(hashFunction.hashBytes(littleEndian), hashFunction.hashBytes(littleEndian)); assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order()); assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order()); }
private static void assertHashBytesEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(bytes), hashFunction.newHasher(size).putBytes(bytes).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(bytes, off, len), hashFunction.newHasher(size).putBytes(bytes, off, len).hash()); }
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 testHashIntReverseBytesVsHashBytesIntsToByteArray() { int input = 42; assertEquals( Hashing.md5().hashBytes(Ints.toByteArray(input)), Hashing.md5().hashInt(Integer.reverseBytes(input))); }
@SuppressWarnings("deprecation") public void testSimpleStringUtf8() { assertEquals( murmur3_32().hashBytes("ABCDefGHI\u0799".getBytes(Charsets.UTF_8)), murmur3_32().hashString("ABCDefGHI\u0799", Charsets.UTF_8)); }
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 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 testInvalidUnicodeHasherPutString() { String str = new String( new char[] {'a', Character.MIN_HIGH_SURROGATE, Character.MIN_HIGH_SURROGATE, 'z'}); assertEquals( murmur3_32().hashBytes(str.getBytes(Charsets.UTF_8)), murmur3_32().newHasher().putString(str, Charsets.UTF_8).hash()); } }
public void testHash_hashesCorrectly() throws Exception { byte[] buf = new byte[] {'y', 'a', 'm', 's'}; HashCode expectedHash = Hashing.md5().hashBytes(buf); HashingOutputStream out = new HashingOutputStream(Hashing.md5(), buffer); out.write(buf); assertEquals(expectedHash, out.hash()); }
public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
private static void assertChecksum(ImmutableSupplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); Checksum checksum = supplier.get(); checksum.update(bytes, 0, bytes.length); long value = checksum.getValue(); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(toString, func.toString()); assertEquals(value, func.hashBytes(bytes).padToLong()); }
private static void assertSip(byte[] input, long expected) { assertEquals(expected, SIP_WITH_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITH_KEY.newHasher().putBytes(input).hash().asLong()); assertEquals(expected, SIP_WITHOUT_KEY.hashBytes(input).asLong()); assertEquals(expected, SIP_WITHOUT_KEY.newHasher().putBytes(input).hash().asLong()); } }
/** * Verifies that the crc of an array of byte data matches the expected value. * * @param expectedCrc the expected crc value. * @param data the data to run the checksum on. */ private static void assertCrc(int expectedCrc, byte[] data) { int actualCrc = Hashing.crc32c().hashBytes(data).asInt(); assertEquals(expectedCrc, actualCrc); }
private static void assertHash32( int expected, ImmutableSupplier<Checksum> supplier, String input) { byte[] bytes = HashTestUtils.ascii(input); String toString = "name"; HashFunction func = new ChecksumHashFunction(supplier, 32, toString); assertEquals(expected, func.hashBytes(bytes).asInt()); assertEquals(toString, func.toString()); } }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }