Search in sources :

Example 76 with Hasher

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);
    }
}
Also used : Hasher(com.google.common.hash.Hasher) CountingOutputStream(com.google.common.io.CountingOutputStream) CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) DataflowPackage(com.google.api.services.dataflow.model.DataflowPackage)

Example 77 with Hasher

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");
    }
}
Also used : Die(com.google.gerrit.common.Die) Hasher(com.google.common.hash.Hasher) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 78 with Hasher

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();
}
Also used : Hasher(com.google.common.hash.Hasher) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) CurrentUser(com.google.gerrit.server.CurrentUser) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) ChangeSet(com.google.gerrit.server.git.ChangeSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 79 with Hasher

use of com.google.common.hash.Hasher in project jackrabbit-oak by apache.

the class GetBlobResponseEncoder method encode.

private static void encode(String blobId, byte[] data, ByteBuf out) {
    byte[] blobIdBytes = blobId.getBytes(Charset.forName("UTF-8"));
    Hasher hasher = Hashing.murmur3_32().newHasher();
    long hash = hasher.putBytes(data).hash().padToLong();
    out.writeInt(1 + 4 + blobIdBytes.length + 8 + data.length);
    out.writeByte(Messages.HEADER_BLOB);
    out.writeInt(blobIdBytes.length);
    out.writeBytes(blobIdBytes);
    out.writeLong(hash);
    out.writeBytes(data);
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 80 with Hasher

use of com.google.common.hash.Hasher in project jackrabbit-oak by apache.

the class GetSegmentResponseEncoder method encode.

private static void encode(String segmentId, byte[] data, ByteBuf out) {
    UUID id = UUID.fromString(segmentId);
    Hasher hasher = Hashing.murmur3_32().newHasher();
    long hash = hasher.putBytes(data).hash().padToLong();
    int len = data.length + EXTRA_HEADERS_WO_SIZE;
    out.writeInt(len);
    out.writeByte(Messages.HEADER_SEGMENT);
    out.writeLong(id.getMostSignificantBits());
    out.writeLong(id.getLeastSignificantBits());
    out.writeLong(hash);
    out.writeBytes(data);
}
Also used : Hasher(com.google.common.hash.Hasher) UUID(java.util.UUID)

Aggregations

Hasher (com.google.common.hash.Hasher)106 HashCode (com.google.common.hash.HashCode)14 IOException (java.io.IOException)12 Test (org.junit.Test)10 HashFunction (com.google.common.hash.HashFunction)6 InputStream (java.io.InputStream)6 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Hcr (org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)4 Hcr (org.apache.servicecomb.it.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.it.edge.encrypt.EncryptContext)4 Buffer (io.vertx.core.buffer.Buffer)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 StringUtils.byteToHexString (org.apache.flink.util.StringUtils.byteToHexString)3 EObject (org.eclipse.emf.ecore.EObject)3