Example usage for com.google.common.hash Hashing md5

List of usage examples for com.google.common.hash Hashing md5

Introduction

In this page you can find the example usage for com.google.common.hash Hashing md5.

Prototype

public static HashFunction md5() 

Source Link

Document

Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to the MD5 MessageDigest .

Usage

From source file:com.google.gerrit.server.change.ChangeResource.java

@Override
public String getETag() {
    CurrentUser user = control.getUser();
    Hasher h = Hashing.md5().newHasher();
    if (user.isIdentifiedUser()) {
        h.putString(starredChangesUtil.getObjectId(user.getAccountId(), getId()).name(), UTF_8);
    }//  w w  w.  java2 s.c o m
    prepareETag(h, user);
    return h.hash().toString();
}

From source file:org.dspace.ctask.replicate.store.DuraCloudObjectStore.java

@Override
public long transferObject(String group, Path file) throws IOException {
    long size = 0L;
    String chkSum = com.google.common.io.Files.hash(file.toFile(), Hashing.md5()).toString(); //Utils.checksum(file, "MD5");
    // make sure this is a different file from what replica store has
    // to avoid network I/O tax
    try {/*  w ww  .j a  v a 2 s . c o  m*/
        Map<String, String> attrs = dcStore.getContentProperties(getSpaceID(group),
                getContentPrefix(group) + file.getFileName().toString());
        if (!chkSum.equals(attrs.get(ContentStore.CONTENT_CHECKSUM))) {
            size = uploadReplica(group, file, chkSum);
        }
    } catch (NotFoundException nfE) {
        // no extant replica - proceed
        size = uploadReplica(group, file, chkSum);
    } catch (ContentStoreException csE) {
        throw new IOException(csE);
    }
    // delete staging file
    Files.delete(file);
    return size;
}

From source file:org.gradle.api.internal.changedetection.state.JvmClassHasher.java

private Hasher createHasher() {
    Hasher hasher = Hashing.md5().newHasher();
    hasher.putBytes(SIGNATURE);
    return hasher;
}

From source file:qa.qcri.nadeef.tools.CommonTools.java

/**
 * Converts a string into an integer hashcode.
 * @param value string value.// w ww . j  a  v  a2  s. c o  m
 * @return integer hashcode.
 */
public static int toHashCode(String value) {
    return Math.abs(Hashing.md5().newHasher().putString(value, Charset.forName("UTF8")).hash().asInt());
}

From source file:org.apache.aurora.scheduler.storage.log.LogStorageModule.java

@Override
protected void configure() {
    bind(Settings.class).toInstance(new Settings(SHUTDOWN_GRACE_PERIOD.get(), SNAPSHOT_INTERVAL.get()));

    bind(new TypeLiteral<Boolean>() {
    }).annotatedWith(ExperimentalTaskStore.class).toInstance(DbModule.USE_DB_TASK_STORE.get());

    bind(new TypeLiteral<Amount<Integer, Data>>() {
    }).annotatedWith(MaxEntrySize.class).toInstance(MAX_LOG_ENTRY_SIZE.get());
    bind(LogManager.class).in(Singleton.class);
    bind(LogStorage.class).in(Singleton.class);

    bind(new TypeLiteral<Set<String>>() {
    }).annotatedWith(HydrateSnapshotFields.class).toInstance(HYDRATE_SNAPSHOT_FIELDS.get());

    install(CallOrderEnforcingStorage.wrappingModule(LogStorage.class));
    bind(DistributedSnapshotStore.class).to(LogStorage.class);
    expose(Storage.class);
    expose(NonVolatileStorage.class);
    expose(DistributedSnapshotStore.class);
    expose(new TypeLiteral<Boolean>() {
    }).annotatedWith(ExperimentalTaskStore.class);
    expose(new TypeLiteral<Set<String>>() {
    }).annotatedWith(HydrateSnapshotFields.class);

    bind(EntrySerializer.class).to(EntrySerializerImpl.class);
    // TODO(ksweeney): We don't need a cryptographic checksum here - assess performance of MD5
    // versus a faster error-detection checksum like CRC32 for large Snapshots.
    bind(HashFunction.class).annotatedWith(LogEntryHashFunction.class).toInstance(Hashing.md5());

    bind(SnapshotDeduplicator.class).to(SnapshotDeduplicatorImpl.class);

    install(new FactoryModuleBuilder().implement(StreamManager.class, StreamManagerImpl.class)
            .build(StreamManagerFactory.class));
}

From source file:net.minecraftforge.fml.relauncher.libraries.Repository.java

public File archive(Artifact artifact, File file, byte[] manifest) {
    File target = artifact.getFile();
    try {/*from w  w  w. j a v  a2  s  . co m*/
        if (target.exists()) {
            FMLLog.log.debug("Maven file already exists for {}({}) at {}, deleting duplicate.", file.getName(),
                    artifact.toString(), target.getAbsolutePath());
            file.delete();
        } else {
            FMLLog.log.debug("Moving file {}({}) to maven repo at {}.", file.getName(), artifact.toString(),
                    target.getAbsolutePath());
            Files.move(file, target);

            if (artifact.isSnapshot()) {
                SnapshotJson json = SnapshotJson.create(artifact.getSnapshotMeta());
                json.add(new SnapshotJson.Entry(artifact.getTimestamp(),
                        Files.hash(target, Hashing.md5()).toString()));
                json.write(artifact.getSnapshotMeta());
            }

            if (!LibraryManager.DISABLE_EXTERNAL_MANIFEST) {
                File meta_target = new File(target.getAbsolutePath() + ".meta");
                Files.write(manifest, meta_target);
            }
        }
        return target;
    } catch (IOException e) {
        FMLLog.log.error(FMLLog.log.getMessageFactory().newMessage("Error moving file {} to {}", file,
                target.getAbsolutePath()), e);
    }
    return file;
}

From source file:org.eclipse.che.api.vfs.impl.file.event.detectors.FileTrackingRegistry.java

private String getHash(String path) {
    try {//from   w  ww .j a v a2  s  .c  om
        final VirtualFile file = vfsProvider.getVirtualFileSystem().getRoot().getChild(Path.of(path));
        String content;
        if (file == null) {
            content = "";
        } else {
            content = file.getContentAsString();
        }

        return Hashing.md5().hashString(content, defaultCharset()).toString();
    } catch (ServerException | ForbiddenException e) {
        LOG.error("Error trying to read {} file and broadcast it", path, e);
    }
    return null;
}

From source file:de.cinovo.cloudconductor.agent.executors.FileExecutor.java

private HashCode getChecksum(String content) {
    return Hashing.md5().hashBytes(content.getBytes());
}

From source file:com.facebook.buck.core.build.engine.manifest.Manifest.java

/** Hash the files pointed to by the source paths. */
@VisibleForTesting// w  ww  .j  a v  a  2  s .  c o  m
static HashCode hashSourcePathGroup(FileHashCache fileHashCache, SourcePathResolver resolver,
        ImmutableList<SourcePath> paths) throws IOException {
    if (paths.size() == 1) {
        return hashSourcePath(paths.get(0), fileHashCache, resolver);
    }
    Hasher hasher = Hashing.md5().newHasher();
    for (SourcePath path : paths) {
        hasher.putBytes(hashSourcePath(path, fileHashCache, resolver).asBytes());
    }
    return hasher.hash();
}

From source file:co.cask.cdap.internal.app.runtime.service.http.HttpHandlerGenerator.java

ClassDefinition generate(TypeToken<? extends HttpServiceHandler> delegateType, String pathPrefix)
        throws IOException {
    Class<?> rawType = delegateType.getRawType();
    List<Class<?>> preservedClasses = Lists.newArrayList();
    preservedClasses.add(rawType);/*from   w ww .  ja  v  a 2 s. c  om*/

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    String internalName = Type.getInternalName(rawType);
    String className = internalName + Hashing.md5().hashString(internalName);

    // Generate the class
    Type classType = Type.getObjectType(className);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className,
            getClassSignature(delegateType), Type.getInternalName(AbstractHttpHandlerDelegator.class), null);

    // Inspect the delegate class hierarchy to generate public handler methods.
    for (TypeToken<?> type : delegateType.getTypes().classes()) {
        if (!Object.class.equals(type.getRawType())) {
            inspectHandler(delegateType, type, pathPrefix, classType, classWriter, preservedClasses);
        }
    }

    generateConstructor(delegateType, classWriter);
    generateLogger(classType, classWriter);

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className,
            preservedClasses);
    // DEBUG block. Uncomment for debug
    //    co.cask.cdap.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}