Java Code Examples for com.google.common.hash.HashCode#asBytes()

The following examples show how to use com.google.common.hash.HashCode#asBytes() . 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: CubaVaadinServletService.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected String toLongNumberString(String data) {
    HashCode hashCode = md5().hashString(data, StandardCharsets.UTF_8);
    byte[] hashBytes = hashCode.asBytes();
    byte[] shortBytes = new byte[Long.BYTES];

    System.arraycopy(hashBytes, 0, shortBytes, 0, Long.BYTES);

    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.put(shortBytes);
    buffer.flip();
    return Long.toString(Math.abs(buffer.getLong()));
}
 
Example 2
Source File: GoogleCloudStorageTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void testObjectVerificationAttributes() throws IOException {
  String bucketName = getSharedBucketName();

  StorageResourceId testObject =
      new StorageResourceId(bucketName, "testObjectValidationAttributes_Object");
  // Don't use hashes in object creation, just validate the round trip. This of course
  // could lead to flaky looking tests due to bit flip errors.
  byte[] objectBytes = writeObject(rawStorage, testObject, /* objectSize= */ 1024);
  GoogleCloudStorageItemInfo itemInfo = rawStorage.getItemInfo(testObject);

  HashCode originalMd5 = Hashing.md5().hashBytes(objectBytes);
  HashCode originalCrc32c = Hashing.crc32c().hashBytes(objectBytes);
  // Note that HashCode#asBytes returns a little-endian encoded array while
  // GCS uses big-endian. We avoid that by grabbing the int value of the CRC32c
  // and running it through Ints.toByteArray which encodes using big-endian.
  byte[] bigEndianCrc32c = Ints.toByteArray(originalCrc32c.asInt());

  GoogleCloudStorageTestHelper.assertByteArrayEquals(
      originalMd5.asBytes(), itemInfo.getVerificationAttributes().getMd5hash());
  // These string versions are slightly easier to debug (used when trying to
  // replicate GCS crc32c values in InMemoryGoogleCloudStorage).
  String originalCrc32cString = Integer.toHexString(Ints.fromByteArray(bigEndianCrc32c));
  String newCrc32cString =
      Integer.toHexString(Ints.fromByteArray(itemInfo.getVerificationAttributes().getCrc32c()));
  assertThat(newCrc32cString).isEqualTo(originalCrc32cString);
  GoogleCloudStorageTestHelper.assertByteArrayEquals(
      bigEndianCrc32c, itemInfo.getVerificationAttributes().getCrc32c());

  VerificationAttributes expectedAttributes =
      new VerificationAttributes(originalMd5.asBytes(), bigEndianCrc32c);

  assertThat(itemInfo.getVerificationAttributes()).isEqualTo(expectedAttributes);
}
 
Example 3
Source File: RemoteActionFileSystemTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Returns a remote artifact and puts its metadata into the action input map. */
private Artifact createRemoteArtifact(
    String pathFragment, String contents, ActionInputMap inputs) {
  Path p = outputRoot.getRoot().asPath().getRelative(pathFragment);
  Artifact a = ActionsTestUtil.createArtifact(outputRoot, p);
  byte[] b = contents.getBytes(StandardCharsets.UTF_8);
  HashCode h = HASH_FUNCTION.getHashFunction().hashBytes(b);
  FileArtifactValue f =
      new RemoteFileArtifactValue(h.asBytes(), b.length, /* locationIndex= */ 1, "action-id");
  inputs.putWithNoDepOwner(a, f);
  return a;
}
 
Example 4
Source File: RemoteActionInputFetcherTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private Artifact createRemoteArtifact(
    String pathFragment,
    String contents,
    Map<ActionInput, FileArtifactValue> metadata,
    Map<Digest, ByteString> cacheEntries) {
  Path p = artifactRoot.getRoot().getRelative(pathFragment);
  Artifact a = ActionsTestUtil.createArtifact(artifactRoot, p);
  byte[] b = contents.getBytes(StandardCharsets.UTF_8);
  HashCode h = HASH_FUNCTION.getHashFunction().hashBytes(b);
  FileArtifactValue f =
      new RemoteFileArtifactValue(h.asBytes(), b.length, /* locationIndex= */ 1, "action-id");
  metadata.put(a, f);
  cacheEntries.put(DigestUtil.buildDigest(h.asBytes(), b.length), ByteString.copyFrom(b));
  return a;
}
 
Example 5
Source File: ByteStreamBuildEventArtifactUploaderTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Returns a remote artifact and puts its metadata into the action input map. */
private Artifact createRemoteArtifact(
    String pathFragment, String contents, ActionInputMap inputs) {
  Path p = outputRoot.getRoot().asPath().getRelative(pathFragment);
  Artifact a = ActionsTestUtil.createArtifact(outputRoot, p);
  byte[] b = contents.getBytes(StandardCharsets.UTF_8);
  HashCode h = HashCode.fromString(DIGEST_UTIL.compute(b).getHash());
  FileArtifactValue f =
      new RemoteFileArtifactValue(h.asBytes(), b.length, /* locationIndex= */ 1, "action-id");
  inputs.putWithNoDepOwner(a, f);
  return a;
}
 
Example 6
Source File: ShortTermDuplicateMemory.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private boolean isProbablyDuplicate(final HashCode eventDigest) {
    // Our hashing algorithm produces 8 bytes:
    //  0: slot[0]
    //  1: slot[1]
    //  2: slot[2]
    //  3: slot[3]
    //  4:
    //  5:
    //  6:
    //  7:
    //  8: signature[0]
    //  9:  ..
    // 10:  ..
    // 11:  ..
    // 12:  ..
    // 13:  ..
    // 14:  ..
    // 15: signature[7]
    final byte[] hashBytes = eventDigest.asBytes();

    // We use the low int for the slot.
    final int slotSelector = Ints.fromBytes(hashBytes[0],
                                            hashBytes[1],
                                            hashBytes[2],
                                            hashBytes[3]);
    // We use the high long for the signature.
    final long signature = Longs.fromBytes(hashBytes[8],
                                           hashBytes[9],
                                           hashBytes[10],
                                           hashBytes[11],
                                           hashBytes[12],
                                           hashBytes[13],
                                           hashBytes[14],
                                           hashBytes[15]);

    final int slot = (slotSelector & Integer.MAX_VALUE) % memory.length;
    final boolean result = memory[slot] == signature;
    memory[slot] = signature;
    return result;
}
 
Example 7
Source File: Sketches.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static BigInteger fromHashCode(HashCode hashCode) {
  return new BigInteger(/*signum=*/ 1, hashCode.asBytes());
}
 
Example 8
Source File: FilesystemValueCheckerTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
private RemoteFileArtifactValue createRemoteFileArtifactValue(String contents) {
  byte[] data = contents.getBytes();
  DigestHashFunction hashFn = fs.getDigestFunction();
  HashCode hash = hashFn.getHashFunction().hashBytes(data);
  return new RemoteFileArtifactValue(hash.asBytes(), data.length, -1, "action-id");
}
 
Example 9
Source File: KeyHasher.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public static String getHashedKey(String key, String hashingAlgorithm) {
    final long start = System.nanoTime();
    HashFunction hf = null; 
    switch(hashingAlgorithm.toLowerCase()) {
        case "murmur3" :
            hf = Hashing.murmur3_128();
            break;

        case "adler32" :
            hf = Hashing.adler32();
            break;

        case "crc32" :
            hf = Hashing.crc32();
            break;

        case "sha1" :
            hf = Hashing.sha1();
            break;

        case "sha256" :
            hf = Hashing.sha256();
            break;

        case "siphash24" :
            hf = Hashing.sipHash24();
            break;

        case "goodfasthash" :
            hf = Hashing.goodFastHash(128);
            break;

        case "md5" :
        default :
            hf = Hashing.md5();
            break;
    }

    final HashCode hc = hf.newHasher().putString(key, Charsets.UTF_8).hash();
    final byte[] digest = hc.asBytes();
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; digest length : " + digest.length + "; byte Array contents : " + Arrays.toString(digest) );
    final String hKey = encoder.encodeToString(digest);
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; Hashed & encoded key : " + hKey + "; Took " + (System.nanoTime() - start) + " nanos");
    return hKey;
}