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

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

Introduction

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

Prototype

public static HashFunction sha384() 

Source Link

Document

Returns a hash function implementing the SHA-384 algorithm (384 hash bits) by delegating to the SHA-384 MessageDigest .

Usage

From source file:com.facebook.buck.randomizedtrial.RandomizedTrial.java

/**
 * This method determines which double number in range of [0.0, 1.0] represents the given key.
 * Algorithm is: 1. Get SHA of the given key. 2. Get first digit of the SHA and convert it into
 * number, e.g. D -> 13. 3. Get 2 digits starting at position we got from step 2, e.g. A1. This is
 * to randomize the resulting value a bit more. 4. Convert these digits into number (A1->161) and
 * divide it by 255 (161/255=0.631) and return this value.
 *
 * @param key Key which point we are looking for.
 * @return Value from 0.0 to 1.0./*w w w  .  j a  v  a 2  s. c o  m*/
 */
private static double getPointForKey(String key) {
    String hash = Hashing.sha384().hashString(key, StandardCharsets.UTF_8).toString();
    Long position = Long.valueOf(hash.substring(0, 1), 16);
    Long byteValue = Long.valueOf(hash.substring(position.intValue() * 2, position.intValue() * 2 + 2), 16);
    double result = byteValue / 255.0;
    LOG.debug("Point for key '%s' is: %f", key, result);
    return result;
}

From source file:com.facebook.buck.util.randomizedtrial.RandomizedTrial.java

/**
 * Return a double uniformly distributed between {@code [0, 1)} derived from the {@code key}.
 *
 * @param key Key which point we are looking for.
 * @return Value from 0.0 (inclusive) to 1.0 (exclusive).
 */// ww  w .  j av  a2s  .c  o m
private static double getPointForKey(String key) {
    return new Random(Hashing.sha384().hashString(key, StandardCharsets.UTF_8).asLong()).nextDouble();
}