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:org.fabrician.enabler.util.DockerfileBuildLock.java

private DockerfileBuildLock(String dockerImageName, File dockerFilePath) throws Exception {
    this.dockerImageName = dockerImageName;
    this.dockerFilePath = dockerFilePath;

    byte[] docker_bytes = FileUtils.readFileToByteArray(dockerFilePath);
    // we create a hash file name from the image and dockerfile content to build with...
    HashFunction hf = Hashing.md5();
    HashCode hc = hf.newHasher().putString(dockerImageName, Charsets.UTF_8).putBytes(docker_bytes).hash();
    String dockerFileHash = BaseEncoding.base64Url().encode(hc.asBytes());
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    lock_file = new File(tmpDir, dockerFileHash + ".dockerfile_lck");
    logger.info("Attempt to acquire Dockerfile build lock at path :" + lock_file);
    lock_channel = FileUtils.openOutputStream(lock_file).getChannel();
    lock = lock_channel.tryLock();/*  ww  w.  j a  v a2s  .  com*/

    if (lock == null) {
        throw new Exception("Can't create exclusive build lock for image [" + dockerImageName
                + "] for Dockerfile [" + dockerFilePath + "]");
    } else {
        logger.info("Acquired Dockerfile build lock at lock path : [" + lock_file + "]");
    }
}

From source file:com.googlesource.gerrit.plugins.xdocs.XDocCache.java

private String getParentsHash(Project.NameKey project) {
    Hasher h = Hashing.md5().newHasher();
    ProjectState p = projectCache.get(project);
    if (p != null) {
        for (ProjectState parent : p.parents()) {
            h.putUnencodedChars(/*w  w  w .  ja  v  a  2  s . c o m*/
                    parent.getConfig().getRevision() != null ? parent.getConfig().getRevision().getName()
                            : ObjectId.zeroId().getName());
        }
    }
    return h.hash().toString();
}

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

@Override
public FileSnapshot snapshot(TextResource resource) {
    File file = resource.getFile();
    if (file != null) {
        return snapshot(file);
    }//from  ww w  .  j  a  v a  2 s.c  o  m
    final HashCode md5 = Hashing.md5().hashString(resource.getText(), Charsets.UTF_8);
    return new FileSnapshot() {
        @Override
        public HashCode getHash() {
            return md5;
        }
    };
}

From source file:co.turnus.versioning.impl.SimpleFileVersioner.java

@Override
public Version getVersion(File file) {
    Date date = new Date();
    Version v = VersioningFactory.eINSTANCE.createVersion();
    v.setVersioningDate(date);//  w ww. j av a2  s.com

    if (file != null && file.exists()) {
        v.setLastModificationDate(new Date(file.lastModified()));
        try {
            v.setId(Files.hash(file, Hashing.md5()).toString());
        } catch (Exception e) {
            v.setId(UUID.randomUUID().toString());
        }
    } else {
        v.setLastModificationDate(date);
        v.setId(UUID.randomUUID().toString());
    }

    v.setAttribute("versioner", NAME);

    return v;
}

From source file:iox.easycache.CachingApplier.java

protected HashFunction createHash() {
    return Hashing.md5();
}

From source file:com.google.gitiles.doc.TocFormatter.java

String idFromHeader(HeaderNode header) {
    String id = ids.get(header);//  w ww  .j  a va  2 s .c o m
    if (id == null) {
        String title = MarkdownUtil.getInnerText(header);
        if (title == null) {
            return null;
        }

        id = idFromTitle(title);
        if (ids.values().contains(id)) {
            id = String.format("%s-%x", id, Hashing.md5().hashString(id, StandardCharsets.UTF_8).asInt());
        }
        ids.put(header, id);
    }
    return id;
}

From source file:lbaas.util.HashAlgorithm.java

public int hash(Object key) {
    HashCode hashCode;/*from   www . ja v  a  2s  .  c om*/
    HashFunction hashAlgorithm;
    switch (hashType) {
    case MD5:
        hashAlgorithm = Hashing.md5();
        break;
    case MURMUR3_32:
        hashAlgorithm = Hashing.murmur3_32();
        break;
    case SHA256:
        hashAlgorithm = Hashing.sha256();
        break;
    case SIP24:
        hashAlgorithm = Hashing.sipHash24();
        break;
    default:
        hashAlgorithm = Hashing.sipHash24();
        break;
    }
    if (key instanceof String) {
        hashCode = hashAlgorithm.newHasher().putString((String) key, Charsets.UTF_8).hash();
    } else if (key instanceof Long) {
        hashCode = hashAlgorithm.newHasher().putLong((Long) key).hash();
    } else {
        hashCode = hashAlgorithm.newHasher().hash();
    }
    return hashCode.asInt();
}

From source file:org.gradle.api.internal.changedetection.rules.TaskTypeTaskStateChanges.java

private static HashCode calculateActionClassLoaderHash(Collection<ClassLoader> taskActionClassLoaders,
        ClassLoaderHierarchyHasher classLoaderHierarchyHasher) {
    if (taskActionClassLoaders.isEmpty()) {
        return NO_ACTION_LOADERS;
    }/*www .  j a v a  2  s . c  om*/
    Hasher hasher = Hashing.md5().newHasher();
    for (ClassLoader taskActionClassLoader : taskActionClassLoaders) {
        HashCode actionLoaderHash = classLoaderHierarchyHasher.getClassLoaderHash(taskActionClassLoader);
        if (actionLoaderHash == null) {
            return null;
        }
        hasher.putBytes(actionLoaderHash.asBytes());
    }
    return hasher.hash();
}

From source file:com.facebook.buck.codegen.SourceSigner.java

/**
 * Given the contents of a source file, checks if a signature is present,
 * and if so, checks if the signature is valid.
 *///from  w ww  .  j av a 2 s  . c o m
public static final SignatureStatus getSignatureStatus(String source) {
    Matcher matcher = SIGNATURE_PATTERN.matcher(source);
    if (!matcher.find()) {
        return SignatureStatus.UNSIGNED;
    }

    if (matcher.groupCount() == 1) {
        String sourceSignature = matcher.group("signature");
        String sourceSignatureReplacedWithToken = matcher.replaceFirst(SIGNED_SOURCE_PLACEHOLDER);
        String expectedSignature = Hashing.md5().hashString(sourceSignatureReplacedWithToken, Charsets.UTF_8)
                .toString();
        if (sourceSignature.equals(expectedSignature)) {
            return SignatureStatus.OK;
        }
    }

    return SignatureStatus.INVALID;
}

From source file:org.rf.ide.core.executor.ArgumentsFile.java

File writeToTemporaryOrUseAlreadyExisting() throws IOException {
    final String content = generateContent();
    // it's deprecated although we don't need security here, so md5 is fine
    @SuppressWarnings("deprecation")
    final HashFunction md5Hasher = Hashing.md5();
    final HashCode hash = md5Hasher.hashString(content, Charsets.UTF_8);

    final String fileName = "args_" + Strings.padStart(Integer.toHexString(hash.asInt()), 8, '0') + ".arg";
    final Path dir = RobotRuntimeEnvironment.createTemporaryDirectory();

    for (final File existingArgFile : dir.toFile().listFiles((d, name) -> name.equals(fileName))) {
        final HashCode candidateHash = md5Hasher
                .hashString(Files.asCharSource(existingArgFile, Charsets.UTF_8).read(), Charsets.UTF_8);
        if (hash.equals(candidateHash)) {
            return existingArgFile;
        }// w w w. ja v a2s .c o  m
    }

    final File filePath = dir.resolve(fileName).toFile();
    writeTo(filePath);
    return filePath;
}