Java Code Examples for com.google.common.hash.HashFunction#hashBytes()

The following examples show how to use com.google.common.hash.HashFunction#hashBytes() . 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: AndroidBinaryIntegrationTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testEditingPrimaryDexClassForcesRebuildForSimplePackage() throws IOException {
  BuildTarget buildTarget = BuildTargetFactory.newInstance(SIMPLE_TARGET);
  Path outputPath = BuildTargetPaths.getGenPath(filesystem, buildTarget, "%s.apk");
  HashFunction hashFunction = Hashing.sha1();
  String dexFileName = "classes.dex";

  workspace.runBuckdCommand("build", SIMPLE_TARGET).assertSuccess();
  ZipInspector zipInspector = new ZipInspector(workspace.getPath(outputPath));
  HashCode originalHash = hashFunction.hashBytes(zipInspector.getFileContents(dexFileName));

  workspace.replaceFileContents(
      "java/com/sample/app/MyApplication.java", "MyReplaceableName", "ChangedValue");

  workspace.resetBuildLogFile();
  ProcessResult result = workspace.runBuckdCommand("build", SIMPLE_TARGET);
  result.assertSuccess();
  BuckBuildLog buildLog = workspace.getBuildLog();
  buildLog.assertTargetBuiltLocally(SIMPLE_TARGET);

  HashCode hashAfterChange = hashFunction.hashBytes(zipInspector.getFileContents(dexFileName));
  assertThat(
      "MyApplication.java file has been edited. Final artifact hash must change as well",
      originalHash,
      is(not(equalTo(hashAfterChange))));
}
 
Example 2
Source File: HmacFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
static Slice computeHash(HashFunction hash, Slice data)
{
    HashCode result;
    if (data.hasByteArray()) {
        result = hash.hashBytes(data.byteArray(), data.byteArrayOffset(), data.length());
    }
    else {
        result = hash.hashBytes(data.getBytes());
    }
    return wrappedBuffer(result.asBytes());
}
 
Example 3
Source File: DexExecTask.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private String getHashFor(File inputFile) {
    String retval = alreadyChecked.get(inputFile.getAbsolutePath());
    if (retval != null) return retval;
    // add a hash of the original file path
    try {
        HashFunction hashFunction = Hashing.md5();
        HashCode hashCode = hashFunction.hashBytes(Files.readAllBytes(inputFile.toPath()));
        retval = hashCode.toString();
        alreadyChecked.put(inputFile.getAbsolutePath(), retval);
        return retval;
    } catch (IOException e) {
        e.printStackTrace();
        return "ERROR";
    }
}
 
Example 4
Source File: Segment.java    From emodb with Apache License 2.0 5 votes vote down vote up
private HashCode hash(ByteBuffer buf) {
    HashFunction hashFn = Hashing.murmur3_128();
    if (buf.hasArray()) {
        return hashFn.hashBytes(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
    } else {
        return hashFn.hashBytes(ByteBufferUtil.getArray(buf));
    }
}
 
Example 5
Source File: MurmurGuavaTest.java    From murmur with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	final int seed = 42;
	HashFunction guava = Hashing.murmur3_128(seed);

	String uuid = UUID.randomUUID().toString();
	byte[] bytes = uuid.getBytes();
	
	HashCode guavaCode = guava.hashBytes(bytes);
	long[] myCodes = Murmur3.hash_x64_128(bytes, bytes.length, seed);
	
	System.out.println("Hash from guava: " + guavaCode);
	System.out.println("Hash from murmur: " + display(myCodes[0]) + " " + display(myCodes[1]));
}
 
Example 6
Source File: ModernBuildRuleRemoteExecutionHelper.java    From buck with Apache License 2.0 5 votes vote down vote up
public byte[] acquireData(Serializer serializer, HashFunction hasher) throws IOException {
  // We should only ever need the data once.
  byte[] current = dataRef.get();
  if (current != null) {
    dropData();
    return current;
  }

  byte[] reserialized = serializer.reserialize(instance);
  HashCode recomputedHash = hasher.hashBytes(reserialized);
  Verify.verify(recomputedHash.toString().equals(hash));
  this.dataRef = new WeakReference<>(reserialized);
  return reserialized;
}
 
Example 7
Source File: GeoIpService.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Verify if the expected checksum is equal to the checksum of the given file.
 *
 * @param function the checksum function like MD5, SHA256 used to generate the checksum from the file
 * @param file the file we want to calculate the checksum from
 * @param expectedChecksum the expected checksum
 * @throws IOException on I/O error reading the file or the checksum verification failed
 */
private void verifyChecksum(HashFunction function, Path file, String expectedChecksum) throws IOException {
    HashCode actualHash = function.hashBytes(Files.readAllBytes(file));
    HashCode expectedHash = HashCode.fromString(expectedChecksum);
    if (!Objects.equals(actualHash, expectedHash)) {
        throw new IOException("GEO IP Checksum verification failed. "
            + "Expected: " + expectedChecksum + "Actual:" + actualHash);
    }
}
 
Example 8
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
  return hashFunction.hashBytes(bytes, offset, length);
}
 
Example 9
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
  return hashFunction.hashBytes(bytes, offset, length);
}
 
Example 10
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
  return hashFunction.hashBytes(bytes, offset, length);
}
 
Example 11
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
  return hashFunction.hashBytes(bytes, offset, length);
}
 
Example 12
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
  return hashFunction.hashBytes(bytes, offset, length);
}
 
Example 13
Source File: ArrayByteSource.java    From Strata with Apache License 2.0 4 votes vote down vote up
@Override
public HashCode hash(HashFunction hashFunction) {
  // overridden to use array directly for performance
  return hashFunction.hashBytes(array);
}
 
Example 14
Source File: MD5HashFileGenerator.java    From spring-soy-view with Apache License 2.0 3 votes vote down vote up
public static String getMD5Checksum(final InputStream is) throws IOException {
    final HashFunction hf = Hashing.md5();

    final HashCode hashCode = hf.hashBytes(getBytesFromInputStream(is));

    return hashCode.toString();
}