Example usage for com.google.common.hash HashCode toString

List of usage examples for com.google.common.hash HashCode toString

Introduction

In this page you can find the example usage for com.google.common.hash HashCode toString.

Prototype

@Override
public final String toString() 

Source Link

Document

Returns a string containing each byte of #asBytes , in order, as a two-digit unsigned hexadecimal number in lower case.

Usage

From source file:com.steeleforge.aem.ironsites.wcm.WCMUtil.java

/**
 * General purpose hashing for Strings such as node path.
 * /*from   w ww  .j a va 2 s.  co  m*/
 * @param token String to hash
 * @param minimumLength minimum length of hash
 * @return hashed value
 * @see <a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/hash/HashCode.html">HashCode</a>
 */
public static String getFastHash(String token, int minimumLength) {
    HashFunction hf = Hashing.goodFastHash(minimumLength);
    HashCode hc = hf.newHasher().putString(token, Charsets.UTF_8).hash();
    return hc.toString();
}

From source file:org.obeonetwork.m2doc.tests.M2DocTestUtils.java

/**
 * Gets the name and hash of the given {@link ZipEntry}.
 * /*from  www.j a v  a  2 s  .c o  m*/
 * @param zin
 *            the {@link ZipInputStream}
 * @param entry
 *            the {@link ZipEntry}
 * @throws IOException
 *             if the {@link ZipInputStream} can't be read
 * @return the name and hash of the given {@link ZipEntry}
 */
private static String getZipEntryHash(ZipInputStream zin, ZipEntry entry) throws IOException {
    final StringBuilder result = new StringBuilder();

    final HashCode code = HF.hashBytes(ByteStreams.toByteArray(zin));
    result.append(String.format("\n%s | ", entry.getName()));
    result.append(String.format("\n md5:%s", code.toString()));

    return result.toString();
}

From source file:com.facebook.buck.features.rust.RustCompileUtils.java

/**
 * Approximate what Cargo does - it computes a hash based on the crate version and its
 * dependencies. Buck will deal with the dependencies and we don't need to worry about the
 * version, but we do need to make sure that two crates with the same name in the build are
 * distinct - so compute the hash from the full target path.
 *
 * @param target Which target we're computing the hash for
 * @return Truncated MD5 hash of the target path
 *//*from   w ww . j  a va  2 s .c o m*/
static String hashForTarget(BuildTarget target) {
    String name = target.getUnflavoredBuildTarget().getFullyQualifiedName();
    Hasher hasher = Hashing.md5().newHasher();
    HashCode hash = hasher.putString(name, StandardCharsets.UTF_8).hash();
    return hash.toString().substring(0, 16);
}

From source file:org.jclouds.glacier.util.AWSRequestSignerV4.java

private static HashCode buildHashedCanonicalRequest(String method, String endpoint, HashCode hashedPayload,
        String canonicalizedHeadersString, String signedHeaders) {
    return Hashing.sha256().newHasher().putString(method, UTF_8).putString("\n", UTF_8)
            .putString(endpoint, UTF_8).putString("\n", UTF_8).putString("\n", UTF_8)
            .putString(canonicalizedHeadersString, UTF_8).putString("\n", UTF_8).putString(signedHeaders, UTF_8)
            .putString("\n", UTF_8).putString(hashedPayload.toString(), UTF_8).hash();
}

From source file:com.android.build.gradle.internal.transforms.DexTransform.java

/**
 * Returns the hash of a file.//from ww w.  j a  va2s.c  o  m
 *
 * If the file is a folder, it's a hash of its path. If the file is a file, then
 * it's a hash of the file itself.
 *
 * @param file the file to hash
 */
@NonNull
private static String getFileHash(@NonNull File file) throws IOException {
    HashCode hashCode;
    HashFunction hashFunction = Hashing.sha1();
    if (file.isDirectory()) {
        hashCode = hashFunction.hashString(file.getPath(), Charsets.UTF_16LE);
    } else {
        hashCode = Files.hash(file, hashFunction);
    }

    return hashCode.toString();
}

From source file:dodola.anole.lib.FileUtils.java

/**
 * Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
 */// w ww  . ja  v a2  s  .  co  m

public static String getDirectoryNameForJar(File inputFile) {
    // add a hash of the original file path.
    HashFunction hashFunction = Hashing.sha1();
    HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charsets.UTF_16LE);

    String name = Files.getNameWithoutExtension(inputFile.getName());
    if (name.equals("classes") && inputFile.getAbsolutePath().contains("exploded-aar")) {
        // This naming scheme is coming from DependencyManager#computeArtifactPath.
        File versionDir = inputFile.getParentFile().getParentFile();
        File artifactDir = versionDir.getParentFile();
        File groupDir = artifactDir.getParentFile();

        name = Joiner.on('-').join(groupDir.getName(), artifactDir.getName(), versionDir.getName());
    }
    name = name + "_" + hashCode.toString();
    return name;
}

From source file:com.android.utils.FileUtils.java

/**
 * Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
 */// www .  j av  a2 s.  c om
@NonNull
public static String getDirectoryNameForJar(@NonNull File inputFile) {
    // add a hash of the original file path.
    HashFunction hashFunction = Hashing.sha1();
    HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charsets.UTF_16LE);

    String name = Files.getNameWithoutExtension(inputFile.getName());
    if (name.equals("classes") && inputFile.getAbsolutePath().contains("exploded-aar")) {
        // This naming scheme is coming from DependencyManager#computeArtifactPath.
        File versionDir = inputFile.getParentFile().getParentFile();
        File artifactDir = versionDir.getParentFile();
        File groupDir = artifactDir.getParentFile();

        name = Joiner.on('-').join(groupDir.getName(), artifactDir.getName(), versionDir.getName());
    }
    name = name + "_" + hashCode.toString();
    return name;
}

From source file:com.facebook.buck.file.RemoteFileBuilder.java

public RemoteFileBuilder setSha1(HashCode hashCode) {
    arg.sha1 = hashCode.toString();
    return this;
}

From source file:edu.cmu.lti.oaqa.framework.eval.Key.java

public String hashString() {
    HashFunction hf = Hashing.sha256();/*from   w w  w.j ava  2s. c  o m*/
    Hasher hasher = hf.newHasher();
    hasher.putString(getExperiment());
    hasher.putString(getTrace().getTrace());
    HashCode hash = hasher.hash();
    return hash.toString();
}

From source file:com.mac.abstractrepository.db.common.IdGenerator.java

public default void generateId(Field idField, String... values)
        throws IllegalArgumentException, IllegalAccessException {
    if (Objects.nonNull(idField) && Objects.nonNull(values) && values.length > 0) {
        Class<?> type = idField.getType();
        if (type == String.class) {
            HashFunction hf = Hashing.md5();
            Hasher hasher = hf.newHasher();
            for (String str : values) {
                if (Objects.isNull(str) || str.isEmpty()) {
                    return;
                }//from  w w  w.j  av a 2  s.c o m
                hasher.putString(str, Charsets.UTF_8);
            }
            HashCode hc = hasher.hash();
            String hash = hc.toString();
            if (Objects.isNull(hash)) {
                return;
            }
            idField.setAccessible(true);
            idField.set(this, hash);
            idField.setAccessible(false);
        }
    }
}