use of com.google.common.hash.Hasher in project guava by google.
the class ByteSource method hash.
/**
* Hashes the contents of this byte source using the given hash function.
*
* @throws IOException if an I/O error occurs while reading from this source
*/
public HashCode hash(HashFunction hashFunction) throws IOException {
Hasher hasher = hashFunction.newHasher();
copyTo(Funnels.asOutputStream(hasher));
return hasher.hash();
}
use of com.google.common.hash.Hasher in project beam by apache.
the class PackageUtil method createPackageAttributes.
/**
* Compute and cache the attributes of a classpath element that we will need to stage it.
*
* @param source the file or directory to be staged.
* @param stagingPath The base location for staged classpath elements.
* @param overridePackageName If non-null, use the given value as the package name
* instead of generating one automatically.
* @return a {@link PackageAttributes} that containing metadata about the object to be staged.
*/
static PackageAttributes createPackageAttributes(File source, String stagingPath, @Nullable String overridePackageName) {
boolean directory = source.isDirectory();
// Compute size and hash in one pass over file or directory.
Hasher hasher = Hashing.md5().newHasher();
OutputStream hashStream = Funnels.asOutputStream(hasher);
try (CountingOutputStream countingOutputStream = new CountingOutputStream(hashStream)) {
if (!directory) {
// Files are staged as-is.
Files.asByteSource(source).copyTo(countingOutputStream);
} else {
// Directories are recursively zipped.
ZipFiles.zipDirectory(source, countingOutputStream);
}
countingOutputStream.flush();
long size = countingOutputStream.getCount();
String hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.hash().asBytes());
// Create the DataflowPackage with staging name and location.
String uniqueName = getUniqueContentName(source, hash);
String resourcePath = FileSystems.matchNewResource(stagingPath, true).resolve(uniqueName, StandardResolveOptions.RESOLVE_FILE).toString();
DataflowPackage target = new DataflowPackage();
target.setName(overridePackageName != null ? overridePackageName : uniqueName);
target.setLocation(resourcePath);
return new PackageAttributes(size, hash, directory, target, source.getPath());
} catch (IOException e) {
throw new RuntimeException("Package setup failure for " + source, e);
}
}
use of com.google.common.hash.Hasher in project gerrit by GerritCodeReview.
the class LibraryDownloader method verifyFileChecksum.
private void verifyFileChecksum() {
if (sha1 == null) {
System.err.println();
System.err.flush();
return;
}
Hasher h = Hashing.sha1().newHasher();
try (InputStream in = Files.newInputStream(dst);
OutputStream out = Funnels.asOutputStream(h)) {
ByteStreams.copy(in, out);
} catch (IOException e) {
deleteDst();
throw new Die("cannot checksum " + dst, e);
}
if (sha1.equals(h.hash().toString())) {
System.err.println("Checksum " + dst.getFileName() + " OK");
System.err.flush();
} else if (ui.isBatch()) {
deleteDst();
throw new Die(dst + " SHA-1 checksum does not match");
} else if (!ui.yesno(null, //
"error: SHA-1 checksum does not match\nUse %s anyway", dst.getFileName())) {
deleteDst();
throw new Die("aborted by user");
}
}
use of com.google.common.hash.Hasher in project gitiles by GerritCodeReview.
the class Renderer method computeTemplateHash.
HashCode computeTemplateHash(String soyFile) {
URL u = templates.get(soyFile);
checkState(u != null, "Missing Soy template %s", soyFile);
Hasher h = Hashing.sha1().newHasher();
try (InputStream is = u.openStream();
OutputStream os = Funnels.asOutputStream(h)) {
ByteStreams.copy(is, os);
} catch (IOException e) {
throw new IllegalStateException("Missing Soy template " + soyFile, e);
}
return h.hash();
}
use of com.google.common.hash.Hasher in project gerrit by GerritCodeReview.
the class GetRevisionActions method getETag.
@Override
public String getETag(RevisionResource rsrc) {
Hasher h = Hashing.md5().newHasher();
CurrentUser user = rsrc.getControl().getUser();
try {
rsrc.getChangeResource().prepareETag(h, user);
h.putBoolean(Submit.wholeTopicEnabled(config));
ReviewDb db = dbProvider.get();
ChangeSet cs = mergeSuperSet.get().completeChangeSet(db, rsrc.getChange(), user);
for (ChangeData cd : cs.changes()) {
changeResourceFactory.create(cd.changeControl()).prepareETag(h, user);
}
h.putBoolean(cs.furtherHiddenChanges());
} catch (IOException | OrmException e) {
throw new OrmRuntimeException(e);
}
return h.hash().toString();
}
Aggregations