Java Code Examples for com.google.common.hash.Hasher#putByte()

The following examples show how to use com.google.common.hash.Hasher#putByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyBufferTest.java    From HaloDB with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "hashes")
public void testHashFinish(HashAlgorithm hashAlgorithm) throws Exception
{

    ByteBuffer buf = ByteBuffer.allocate(12);
    byte[] ref = TestUtils.generateRandomByteArray(10);
    buf.put((byte) (42 & 0xff));
    buf.put(ref);
    buf.put((byte) (0xf0 & 0xff));

    KeyBuffer out = new KeyBuffer(buf.array());
    out.finish(com.oath.halodb.Hasher.create(hashAlgorithm));

    Hasher hasher = hasher(hashAlgorithm);
    hasher.putByte((byte) 42);
    hasher.putBytes(ref);
    hasher.putByte((byte) 0xf0);
    long longHash = hash(hasher);

    assertEquals(out.hash(), longHash);
}
 
Example 2
Source File: KeyBufferTest.java    From HaloDB with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "hashes", dependsOnMethods = "testHashFinish")
public void testHashFinish16(HashAlgorithm hashAlgorithm) throws Exception
{

    byte[] ref = TestUtils.generateRandomByteArray(14);
    ByteBuffer buf = ByteBuffer.allocate(16);
    buf.put((byte) (42 & 0xff));
    buf.put(ref);
    buf.put((byte) (0xf0 & 0xff));
    KeyBuffer out = new KeyBuffer(buf.array());
    out.finish(com.oath.halodb.Hasher.create(hashAlgorithm));

    Hasher hasher = hasher(hashAlgorithm);
    hasher.putByte((byte) 42);
    hasher.putBytes(ref);
    hasher.putByte((byte) 0xf0);
    long longHash = hash(hasher);

    assertEquals(out.hash(), longHash);
}
 
Example 3
Source File: NullHasherTest.java    From elastic-load-balancing-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasics() {
    Hasher hasher = NullHasher.INSTANCE;
    assertEquals(0, hasher.hash().asInt());
    hasher.putBoolean(false);
    hasher.putByte((byte) 3);
    hasher.putBytes(new byte[0]);
    hasher.putBytes(null, 3, 3);
    hasher.putChar('c');
    hasher.putDouble(3.3);
    hasher.putFloat(3.4f);
    hasher.putInt(7);
    hasher.putLong(3);
    hasher.putObject(null, null);
    hasher.putShort((short) 7);
    hasher.putString(null, null);
    hasher.putUnencodedChars(null);
}
 
Example 4
Source File: DefaultJarFileStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private static Function<File, String> hashWith(Supplier<HashFunction> algorithm) {
    return (file) -> {
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            Hasher hasher = algorithm.get().newHasher();
            int b;
            while ((b = inputStream.read()) != -1) {
                hasher.putByte((byte) b);
            }
            return hasher.hash().toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
 
Example 5
Source File: BinaryParser.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
public Hasher init(final URI url) {
	final Hasher hasher = hashFunction.newHasher();
	if (url != null) {
		// Note that we need to go directly to the hasher to encode explicit IP addresses
		hasher.putUnencodedChars(url.getHost());
		hasher.putByte((byte)0);
	}
	return hasher;
}
 
Example 6
Source File: MultiHashingInputStream.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int read() throws IOException {
  int b = in.read();
  if (b != -1) {
    for (Hasher hasher : hashers.values()) {
      hasher.putByte((byte) b);
    }
    count++;
  }
  return b;
}
 
Example 7
Source File: MultiHashingOutputStream.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void write(final int b) throws IOException {
  out.write(b);
  for (Hasher hasher : hashers.values()) {
    hasher.putByte((byte)b);
  }
}
 
Example 8
Source File: ConfigFeatureFlagConfiguration.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Converts the given flag values into a string hash for use as an output directory fragment. */
private static String hashFlags(SortedMap<Label, String> flagValues) {
  // This hash function is relatively fast and stable between JVM invocations.
  Hasher hasher = Hashing.murmur3_128().newHasher();

  for (Map.Entry<Label, String> flag : flagValues.entrySet()) {
    hasher.putUnencodedChars(flag.getKey().toString());
    hasher.putByte((byte) 0);
    hasher.putUnencodedChars(flag.getValue());
    hasher.putByte((byte) 0);
  }
  return hasher.hash().toString();
}