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

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

Introduction

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

Prototype


@CheckReturnValue
public abstract byte[] asBytes();

Source Link

Document

Returns the value of this hash code as a byte array.

Usage

From source file:de.schildbach.pte.AbstractHafasMobileProvider.java

private HttpUrl requestUrl(final String body) {
    final HttpUrl.Builder url = checkNotNull(mgateEndpoint).newBuilder();
    if (requestChecksumSalt != null) {
        final HashCode checksum = MD5.newHasher().putString(body, Charsets.UTF_8)
                .putString(requestChecksumSalt, Charsets.UTF_8).hash();
        url.addQueryParameter("checksum", checksum.toString());
    }//ww  w .j a  v  a  2s .  c o  m
    if (requestMicMacSalt != null) {
        final HashCode mic = MD5.newHasher().putString(body, Charsets.UTF_8).hash();
        url.addQueryParameter("mic", HEX.encode(mic.asBytes()));
        final HashCode mac = MD5.newHasher().putString(HEX.encode(mic.asBytes()), Charsets.UTF_8)
                .putBytes(HEX.decode(requestMicMacSalt)).hash();
        url.addQueryParameter("mac", HEX.encode(mac.asBytes()));
    }
    return url.build();
}

From source file:de.brendamour.jpasskit.signing.PKFileBasedSigningUtil.java

private void hashFilesInDirectory(final File[] files, final Map<String, String> fileWithHashMap,
        final HashFunction hashFunction, final String parentName) throws PKSigningException {
    StringBuilder name;//from   w  w w . j a  v a  2 s  .c o m
    HashCode hash;
    for (File passResourceFile : files) {
        name = new StringBuilder();
        if (passResourceFile.isFile()) {
            try {
                hash = Files.hash(passResourceFile, hashFunction);
            } catch (IOException e) {
                throw new PKSigningException("Error when hashing files", e);
            }
            if (StringUtils.isEmpty(parentName)) {
                // direct call
                name.append(passResourceFile.getName());
            } else {
                // recursive call (apeending parent directory)
                name.append(parentName);
                name.append(FILE_SEPARATOR_UNIX);
                name.append(passResourceFile.getName());
            }
            fileWithHashMap.put(name.toString(), Hex.encodeHexString(hash.asBytes()));
        } else if (passResourceFile.isDirectory()) {
            if (StringUtils.isEmpty(parentName)) {
                // direct call
                name.append(passResourceFile.getName());
            } else {
                // recursive call (apeending parent directory)
                name.append(parentName);
                name.append(FILE_SEPARATOR_UNIX);
                name.append(passResourceFile.getName());
            }
            hashFilesInDirectory(passResourceFile.listFiles(), fileWithHashMap, hashFunction, name.toString());
        }
    }
}

From source file:org.jclouds.jdbc.service.JdbcService.java

@Transactional(rollbackOn = IOException.class)
public BlobEntity createOrModifyBlob(String containerName, Blob blob, BlobAccess blobAccess)
        throws IOException {
    List<Long> chunks;
    HashingInputStream his = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream());
    try {//  ww w .ja v  a 2 s.com
        chunks = storeData(his);
    } finally {
        Closeables2.closeQuietly(his);
    }
    HashCode actualHashCode = his.hash();
    HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode();
    if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) {
        throw new IOException(
                "MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode);
    }

    String key = blob.getMetadata().getName();
    Date creationDate = null;
    BlobEntity oldBlobEntity = findBlobById(containerName, key);
    if (oldBlobEntity != null) {
        creationDate = oldBlobEntity.getCreationDate();
    }
    BlobEntity blobEntity = blobToBlobEntity.apply(blob);
    blobEntity.getPayload().setChunks(chunks);
    blobEntity.setContainerEntity(containerRepository.findContainerByName(containerName));
    blobEntity.setKey(key);
    blobEntity.setBlobAccess(blobAccess);
    blobEntity.setTier(blob.getMetadata().getTier());
    blobEntity.setCreationDate(creationDate);
    blobEntity.setLastModified(new Date());
    blobEntity.setEtag(base16().lowerCase().encode(actualHashCode.asBytes()));
    blobEntity.getPayload().setContentMD5(actualHashCode.asBytes());

    BlobEntity result = blobRepository.save(blobEntity);
    return result;
}

From source file:com.github.mgunlogson.cuckoofilter4j.BucketAndTag.java

/**
 * Generates the Bucket Index and Tag for a given item. Handling is
 * different for 32,64,and 128+ hashes to best use the number of bits
 * available. Specifically for 32 and 64 bit hashes we need to shift off
 * bits for the tag and index since they are bigger than the hash (they are
 * longs...64 bits each). For anything less than 128 bit hashes there is a
 * limit to (bucket number + tag bits) for this reason. The
 * {@code #getTotalBitsNeeded(long, int) in
 * {@code #isHashConfigurationIsSupported(long, int, int)} makes sure we
 * have enough bits for the filter size when the table is constructed.
 * /*from ww w .j a v  a 2  s .co  m*/
 */
BucketAndTag generate(T item) {
    /*
     * How do we get tag and bucketIndex from a single 32 bit hash? Max
     * filter size is constrained to 32 bits of bits (by BitSet) So, the bit
     * offset for any bit cannot exceed 32 bit boundary. Since max bit
     * offset is BUCKET_SIZE*bucketIndex*tagBits, we can never use more than
     * 32 bits of hash for tagBits+bucketIndex
     */
    long tag = 0;
    long bucketIndex = 0;
    HashCode code = hasher.hashObj(item);
    // 32 bit hash
    //      if (hashLength == 32) {
    //         int hashVal = code.asInt();
    //         bucketIndex = getBucketIndex32(hashVal);
    //         // loop until tag isn't equal to empty bucket (0)
    //         tag = getTagValue32(hashVal);
    //         for (int salt = 1; tag == 0; salt++) {
    //            hashVal = hasher.hashObjWithSalt(item, salt).asInt();
    //            tag = getTagValue32(hashVal);
    //            assert salt < 100;// shouldn't happen in our timeline
    //         }
    //      } else 
    if (hashLength == 64) {//TODO fix hashLength 32 not 64
        long hashVal = code.asLong();
        bucketIndex = getBucketIndex64(hashVal);
        // loop until tag isn't equal to empty bucket (0)
        tag = getTagValue64(hashVal);
        for (int salt = 1; tag == 0; salt++) {
            hashVal = hasher.hashObjWithSalt(item, salt).asLong();
            tag = getTagValue64(hashVal);
            assert salt < 100;// shouldn't happen in our timeline
        }
    }
    // >=128
    else {
        byte[] hashVal = code.asBytes();
        bucketIndex = getBucketIndex64(longFromLowBytes(hashVal));
        // loop until tag isn't equal to empty bucket (0)
        tag = getTagValue64(longFromHighBytes(hashVal));
        for (int salt = 1; tag == 0; salt++) {
            hashVal = hasher.hashObjWithSalt(item, salt).asBytes();
            tag = getTagValue64(longFromHighBytes(hashVal));
            assert salt < 100;// shouldn't happen in our timeline
        }
    }
    return new BucketAndTag(bucketIndex, tag);
}