use of com.google.common.hash.Hasher in project bazel by bazelbuild.
the class RepositoryCache method getChecksum.
/**
* Obtain the checksum of a file.
*
* @param keyType The type of hash function. e.g. SHA-1, SHA-256.
* @param path The path to the file.
* @throws IOException
*/
public static String getChecksum(KeyType keyType, Path path) throws IOException {
Hasher hasher = keyType.newHasher();
byte[] byteBuffer = new byte[BUFFER_SIZE];
try (InputStream stream = path.getInputStream()) {
int numBytesRead = stream.read(byteBuffer);
while (numBytesRead != -1) {
if (numBytesRead != 0) {
// If more than 0 bytes were read, add them to the hash.
hasher.putBytes(byteBuffer, 0, numBytesRead);
}
numBytesRead = stream.read(byteBuffer);
}
}
return hasher.hash().toString();
}
use of com.google.common.hash.Hasher in project bazel by bazelbuild.
the class WorkerFilesHash method getWorkerFilesHash.
public static HashCode getWorkerFilesHash(Iterable<? extends ActionInput> toolFiles, ActionExecutionContext actionExecutionContext) throws IOException {
Hasher hasher = Hashing.sha256().newHasher();
for (ActionInput tool : toolFiles) {
hasher.putString(tool.getExecPathString(), Charset.defaultCharset());
hasher.putBytes(actionExecutionContext.getActionInputFileCache().getDigest(tool));
}
return hasher.hash();
}
use of com.google.common.hash.Hasher in project che by eclipse.
the class ETagResponseFilter method doFilter.
/**
* Filter the given container response
*
* @param containerResponse
* the response to use
*/
public void doFilter(GenericContainerResponse containerResponse) {
// get entity of the response
Object entity = containerResponse.getEntity();
// no entity, skip
if (entity == null) {
return;
}
// Only handle JSON content
if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
return;
}
// Get the request
ApplicationContext applicationContext = ApplicationContext.getCurrent();
Request request = applicationContext.getRequest();
// manage only GET requests
if (!HttpMethod.GET.equals(request.getMethod())) {
return;
}
// calculate hash with MD5
HashFunction hashFunction = Hashing.md5();
Hasher hasher = hashFunction.newHasher();
boolean hashingSuccess = true;
// Manage a list
if (entity instanceof List) {
List<?> entities = (List) entity;
for (Object simpleEntity : entities) {
hashingSuccess = addHash(simpleEntity, hasher);
if (!hashingSuccess) {
break;
}
}
} else {
hashingSuccess = addHash(entity, hasher);
}
// if we're able to handle the hash
if (hashingSuccess) {
// get result of the hash
HashCode hashCode = hasher.hash();
// Create the entity tag
EntityTag entityTag = new EntityTag(hashCode.toString());
// Check the etag
Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);
// not modified ?
if (builder != null) {
containerResponse.setResponse(builder.tag(entityTag).build());
} else {
// it has been changed, so send response with new ETag and entity
Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
containerResponse.setResponse(responseBuilder.build());
}
}
}
use of com.google.common.hash.Hasher in project buck by facebook.
the class VersionedTargetGraphBuilder method getVersionedFlavor.
/**
* @return a flavor to which summarizes the given version selections.
*/
static Flavor getVersionedFlavor(SortedMap<BuildTarget, Version> versions) {
Preconditions.checkArgument(!versions.isEmpty());
Hasher hasher = Hashing.md5().newHasher();
for (Map.Entry<BuildTarget, Version> ent : versions.entrySet()) {
hasher.putString(ent.getKey().toString(), Charsets.UTF_8);
hasher.putString(ent.getValue().getName(), Charsets.UTF_8);
}
return InternalFlavor.of("v" + hasher.hash().toString().substring(0, 7));
}
use of com.google.common.hash.Hasher in project buck by facebook.
the class JsonObjectHashingTest method nullFieldsAreIgnoredInTheHash.
/**
* This helps keep the hashes more stable. While we include the buckversion in target hashes, when
* making changes within Buck (such as adding or removing constructor arg fields) this means that
* there are fewer places where tests fail, meaning less time being spent debugging "failed"
* tests.
* <p>
* This means that two maps may generate the same hashcode, despite not being strictly equal. We
* rely on the use case that this class was designed for (namely, generating target hashes) to
* ensure that correct functionality is maintained.
*/
@Test
public void nullFieldsAreIgnoredInTheHash() {
HashMap<String, Object> map1 = new HashMap<>();
map1.put("firstKey", "value");
map1.put("secondKey", null);
HashMap<String, Object> map2 = new HashMap<>();
map2.put("firstKey", "value");
map2.put("ignoredKey", null);
Hasher hasher1 = Hashing.sha1().newHasher();
Hasher hasher2 = Hashing.sha1().newHasher();
JsonObjectHashing.hashJsonObject(hasher1, map1);
JsonObjectHashing.hashJsonObject(hasher2, map2);
assertEquals(hasher1.hash().toString(), hasher2.hash().toString());
}
Aggregations