Example usage for com.google.common.hash HashFunction hashBytes

List of usage examples for com.google.common.hash HashFunction hashBytes

Introduction

In this page you can find the example usage for com.google.common.hash HashFunction hashBytes.

Prototype

HashCode hashBytes(byte[] input);

Source Link

Document

Shortcut for newHasher().putBytes(input).hash() .

Usage

From source file:com.stackframe.sarariman.PhotoFetcher.java

private static String entityTag(byte[] b) {
    HashFunction hashFunction = Hashing.md5();
    String hash = hashFunction.hashBytes(b).toString();
    return String.format("\"%s\"", hash);
}

From source file:com.facebook.buck.apple.toolchain.impl.ProvisioningProfileMetadataFactory.java

public static ProvisioningProfileMetadata fromProvisioningProfilePath(ProcessExecutor executor,
        ImmutableList<String> readCommand, Path profilePath) throws IOException, InterruptedException {
    Set<Option> options = EnumSet.of(Option.EXPECTING_STD_OUT);

    // Extract the XML from its signed message wrapper.
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(readCommand)
            .addCommand(profilePath.toString()).build();
    ProcessExecutor.Result result;
    result = executor.launchAndExecute(processExecutorParams, options, /* stdin */ Optional.empty(),
            /* timeOutMs */ Optional.empty(), /* timeOutHandler */ Optional.empty());
    if (result.getExitCode() != 0) {
        throw new IOException(result.getMessageForResult("Invalid provisioning profile: " + profilePath));
    }/*  w w  w  . j a v  a 2 s.c  om*/

    try {
        NSDictionary plist = (NSDictionary) PropertyListParser.parse(result.getStdout().get().getBytes());
        Date expirationDate = ((NSDate) plist.get("ExpirationDate")).getDate();
        String uuid = ((NSString) plist.get("UUID")).getContent();

        ImmutableSet.Builder<HashCode> certificateFingerprints = ImmutableSet.builder();
        NSArray certificates = (NSArray) plist.get("DeveloperCertificates");
        HashFunction hasher = Hashing.sha1();
        if (certificates != null) {
            for (NSObject item : certificates.getArray()) {
                certificateFingerprints.add(hasher.hashBytes(((NSData) item).bytes()));
            }
        }

        ImmutableMap.Builder<String, NSObject> builder = ImmutableMap.builder();
        NSDictionary entitlements = ((NSDictionary) plist.get("Entitlements"));
        for (String key : entitlements.keySet()) {
            builder = builder.put(key, entitlements.objectForKey(key));
        }
        String appID = entitlements.get("application-identifier").toString();

        ProvisioningProfileMetadata.Builder provisioningProfileMetadata = ProvisioningProfileMetadata.builder();
        if (plist.get("Platform") != null) {
            for (Object platform : (Object[]) plist.get("Platform").toJavaObject()) {
                provisioningProfileMetadata.addPlatforms((String) platform);
            }
        }
        return provisioningProfileMetadata.setAppID(ProvisioningProfileMetadata.splitAppID(appID))
                .setExpirationDate(expirationDate).setUUID(uuid).setProfilePath(profilePath)
                .setEntitlements(builder.build())
                .setDeveloperCertificateFingerprints(certificateFingerprints.build()).build();
    } catch (Exception e) {
        throw new IllegalArgumentException("Malformed embedded plist: " + e);
    }
}

From source file:com.facebook.buck.apple.AbstractProvisioningProfileMetadata.java

public static ProvisioningProfileMetadata fromProvisioningProfilePath(ProcessExecutor executor,
        ImmutableList<String> readCommand, Path profilePath) throws IOException, InterruptedException {
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);

    // Extract the XML from its signed message wrapper.
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(readCommand)
            .addCommand(profilePath.toString()).build();
    ProcessExecutor.Result result;
    result = executor.launchAndExecute(processExecutorParams, options, /* stdin */ Optional.empty(),
            /* timeOutMs */ Optional.empty(), /* timeOutHandler */ Optional.empty());
    if (result.getExitCode() != 0) {
        throw new IOException(result.getMessageForResult("Invalid provisioning profile: " + profilePath));
    }/*from w ww  . ja va 2  s .c om*/

    try {
        NSDictionary plist = (NSDictionary) PropertyListParser.parse(result.getStdout().get().getBytes());
        Date expirationDate = ((NSDate) plist.get("ExpirationDate")).getDate();
        String uuid = ((NSString) plist.get("UUID")).getContent();

        ImmutableSet.Builder<HashCode> certificateFingerprints = ImmutableSet.builder();
        NSArray certificates = (NSArray) plist.get("DeveloperCertificates");
        HashFunction hasher = Hashing.sha1();
        if (certificates != null) {
            for (NSObject item : certificates.getArray()) {
                certificateFingerprints.add(hasher.hashBytes(((NSData) item).bytes()));
            }
        }

        ImmutableMap.Builder<String, NSObject> builder = ImmutableMap.builder();
        NSDictionary entitlements = ((NSDictionary) plist.get("Entitlements"));
        for (String key : entitlements.keySet()) {
            builder = builder.put(key, entitlements.objectForKey(key));
        }
        String appID = entitlements.get("application-identifier").toString();

        return ProvisioningProfileMetadata.builder().setAppID(ProvisioningProfileMetadata.splitAppID(appID))
                .setExpirationDate(expirationDate).setUUID(uuid).setProfilePath(profilePath)
                .setEntitlements(builder.build())
                .setDeveloperCertificateFingerprints(certificateFingerprints.build()).build();
    } catch (Exception e) {
        throw new IllegalArgumentException("Malformed embedded plist: " + e);
    }
}

From source file:com.google.cloud.storage.spi.DefaultStorageRpc.java

private static void setEncryptionHeaders(HttpHeaders headers, String headerPrefix, Map<Option, ?> options) {
    String key = CUSTOMER_SUPPLIED_KEY.getString(options);
    if (key != null) {
        BaseEncoding base64 = BaseEncoding.base64();
        HashFunction hashFunction = Hashing.sha256();
        headers.set(headerPrefix + "algorithm", "AES256");
        headers.set(headerPrefix + "key", key);
        headers.set(headerPrefix + "key-sha256",
                base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
    }//from   www . jav  a2  s.  co  m
}

From source file:io.seldon.vw.VwFeatureHash.java

public Integer getFeatureHash(int label, String namespace, String feature) {
    int nsHash = 0;
    if (!StringUtils.isEmpty(namespace)) {
        HashFunction h = Hashing.murmur3_32(0);
        nsHash = h.hashBytes(namespace.getBytes()).asInt();
    }/*from   w  w w. j a  v  a 2  s. com*/
    int hcl = 0;
    if (isInteger(feature))
        hcl = Integer.parseInt(feature) + nsHash;
    else {
        HashFunction h = Hashing.murmur3_32(nsHash);
        hcl = (h.hashBytes(feature.getBytes()).asInt());
    }
    int f = ((hcl * stride) + label - 1) & mask;
    return f;
}

From source file:com.google.cloud.storage.spi.v1.HttpStorageRpc.java

private static void setEncryptionHeaders(HttpHeaders headers, String headerPrefix, Map<Option, ?> options) {
    String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
    if (key != null) {
        BaseEncoding base64 = BaseEncoding.base64();
        HashFunction hashFunction = Hashing.sha256();
        headers.set(headerPrefix + "algorithm", "AES256");
        headers.set(headerPrefix + "key", key);
        headers.set(headerPrefix + "key-sha256",
                base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
    }/*  ww w . j  a v a  2 s .c o m*/
}

From source file:com.tinspx.util.io.ByteArrayChannelSource.java

@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
    return hashFunction.hashBytes(read());
}

From source file:net.derquinse.common.io.ByteArrayByteSource.java

@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
    return hashFunction.hashBytes(bytes);
}

From source file:org.breizhbeans.thrift.tools.thriftmongobridge.example.SecuredWrapper.java

@Override
public long digest64(byte[] data) {
    // initialize the hash with the AES Hash code
    int seed = Arrays.hashCode(skeyspec.getEncoded());
    HashFunction hf = Hashing.murmur3_128(seed);

    com.google.common.hash.HashCode hashCode = hf.hashBytes(data);

    return hashCode.padToLong();
}

From source file:net.derquinse.common.io.EmptyByteSource.java

@Override
public HashCode hash(HashFunction hashFunction) throws IOException {
    return hashFunction.hashBytes(EmptyInputStream.EMPTY_ARRAY);
}