Example usage for com.google.common.hash Hasher hash

List of usage examples for com.google.common.hash Hasher hash

Introduction

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

Prototype

@CheckReturnValue
HashCode hash();

Source Link

Document

Computes a hash code based on the data that have been provided to this hasher.

Usage

From source file:teetime.stage.MD5Stage.java

@Override
protected void execute(final String element) {
    Hasher hasher = Hashing.md5().newHasher();
    hasher.putString(element, charset);//from   w  w  w. j  av  a 2  s  .c  om
    outputPort.send(hasher.hash().toString());
}

From source file:org.gradle.api.internal.hash.DefaultFileHasher.java

@Override
public HashCode hash(File file) {
    try {/*  w  w  w  .j  a  v  a  2s .co  m*/
        Hasher hasher = createFileHasher();
        Files.copy(file, Funnels.asOutputStream(hasher));
        return hasher.hash();
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Failed to create MD5 hash for file '%s'.", file), e);
    }
}

From source file:documents.Document.java

public int hashCode() {
    Hasher hf = Hashing.md5().newHasher();
    metadata.forEach((k, v) -> hf.putString(v, Charset.defaultCharset()));
    return hf.hash().asInt();
}

From source file:com.dssmp.agent.tailing.KinesisRecord.java

@VisibleForTesting
String generatePartitionKey(PartitionKeyOption option) {
    Preconditions.checkNotNull(option);/*from  w w  w.  j a va2  s. c o  m*/

    if (option == PartitionKeyOption.DETERMINISTIC) {
        Hasher hasher = Hashing.md5().newHasher();
        hasher.putBytes(data.array());
        return hasher.hash().toString();
    }
    if (option == PartitionKeyOption.RANDOM)
        return "" + ThreadLocalRandom.current().nextDouble(1000000);

    return null;
}

From source file:org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl.java

/**
 * compute pseudorandom key unique for given seed and {@link #auxiliaryId}
 * @param seed random int but fixed per session
 *//* www .java2  s  .  c om*/
public void init(int seed) {
    if (auxiliaryId <= 0) {
        throw new IllegalStateException("auxiliaryId must be greater than 0");
    }

    HashFunction mm32Hf = Hashing.murmur3_32(seed);
    Hasher hasher = mm32Hf.newHasher(8);
    hasher.putInt(auxiliaryId);
    long hash = 0xFFFFFFFFL & hasher.hash().asInt();
    cookie = (auxiliaryId << 24) | (hash >> 8);
}

From source file:org.apache.kylin.cube.util.CubingUtils.java

public static Map<Long, HLLCounter> sampling(CubeDesc cubeDesc, IJoinedFlatTableDesc flatDescIn,
        Iterable<List<String>> streams) {
    final CubeJoinedFlatTableEnrich flatDesc = new CubeJoinedFlatTableEnrich(flatDescIn, cubeDesc);
    final int rowkeyLength = cubeDesc.getRowkey().getRowKeyColumns().length;
    final List<Long> allCuboidIds = new CuboidScheduler(cubeDesc).getAllCuboidIds();
    final long baseCuboidId = Cuboid.getBaseCuboidId(cubeDesc);
    final Map<Long, Integer[]> allCuboidsBitSet = Maps.newHashMap();

    Lists.transform(allCuboidIds, new Function<Long, Integer[]>() {
        @Nullable/*from w w  w  .  j  a va2s  .  c  o  m*/
        @Override
        public Integer[] apply(@Nullable Long cuboidId) {
            Integer[] result = new Integer[Long.bitCount(cuboidId)];

            long mask = Long.highestOneBit(baseCuboidId);
            int position = 0;
            for (int i = 0; i < rowkeyLength; i++) {
                if ((mask & cuboidId) > 0) {
                    result[position] = i;
                    position++;
                }
                mask = mask >> 1;
            }
            return result;
        }
    });
    final Map<Long, HLLCounter> result = Maps.newHashMapWithExpectedSize(allCuboidIds.size());
    for (Long cuboidId : allCuboidIds) {
        result.put(cuboidId, new HLLCounter(cubeDesc.getConfig().getCubeStatsHLLPrecision()));
        Integer[] cuboidBitSet = new Integer[Long.bitCount(cuboidId)];

        long mask = Long.highestOneBit(baseCuboidId);
        int position = 0;
        for (int i = 0; i < rowkeyLength; i++) {
            if ((mask & cuboidId) > 0) {
                cuboidBitSet[position] = i;
                position++;
            }
            mask = mask >> 1;
        }
        allCuboidsBitSet.put(cuboidId, cuboidBitSet);
    }

    HashFunction hf = Hashing.murmur3_32();
    ByteArray[] row_hashcodes = new ByteArray[rowkeyLength];
    for (int i = 0; i < rowkeyLength; i++) {
        row_hashcodes[i] = new ByteArray();
    }
    for (List<String> row : streams) {
        //generate hash for each row key column
        for (int i = 0; i < rowkeyLength; i++) {
            Hasher hc = hf.newHasher();
            final String cell = row.get(flatDesc.getRowKeyColumnIndexes()[i]);
            if (cell != null) {
                row_hashcodes[i].set(hc.putString(cell).hash().asBytes());
            } else {
                row_hashcodes[i].set(hc.putInt(0).hash().asBytes());
            }
        }

        for (Map.Entry<Long, HLLCounter> longHyperLogLogPlusCounterNewEntry : result.entrySet()) {
            Long cuboidId = longHyperLogLogPlusCounterNewEntry.getKey();
            HLLCounter counter = longHyperLogLogPlusCounterNewEntry.getValue();
            Hasher hc = hf.newHasher();
            final Integer[] cuboidBitSet = allCuboidsBitSet.get(cuboidId);
            for (int position = 0; position < cuboidBitSet.length; position++) {
                hc.putBytes(row_hashcodes[cuboidBitSet[position]].array());
            }
            counter.add(hc.hash().asBytes());
        }
    }
    return result;
}

From source file:com.underthehood.weblogs.utils.TimeuuidGenerator.java

/**
 * Using MAC address, process id, and some env props 
 * @param addr//from   ww  w  .  ja v  a 2s. c  o  m
 * @return
 */
private static long makeNode(InetAddress addr) {
    byte[] mac = null;
    if (addr != null) {
        try {
            mac = NetworkInterface.getByInetAddress(addr).getHardwareAddress();

        } catch (SocketException e) {
        }
    }
    if (mac == null) {
        try {
            mac = NetworkInterface.getByInetAddress(getActualIPv4HostAddress()).getHardwareAddress();

            Hasher hash = Hashing.md5().newHasher();
            hash.putBytes(mac);

            String procId = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
            hash.putBytes(procId.getBytes(StandardCharsets.UTF_8));

            Properties props = System.getProperties();
            hash.putBytes(props.getProperty("os.arch").getBytes(StandardCharsets.UTF_8));
            hash.putBytes(props.getProperty("os.name").getBytes(StandardCharsets.UTF_8));
            hash.putBytes(props.getProperty("os.version").getBytes(StandardCharsets.UTF_8));
            hash.putBytes(props.getProperty("java.vendor").getBytes(StandardCharsets.UTF_8));
            hash.putBytes(props.getProperty("java.version").getBytes(StandardCharsets.UTF_8));

            return hash.hash().asLong();

        } catch (SocketException e) {
            log.warn("Unable to get a valid network interface!");
            log.debug("", e);
        }
    }
    return UUID.randomUUID().getLeastSignificantBits();
}

From source file:io.github.maxymania.powercache.proxy.MethodCall.java

@Override
public int hashCode() {
    //int hash = 7;
    //hash = 79 * hash + Objects.hashCode(this.name);
    //hash = 79 * hash + Arrays.deepHashCode(this.data);
    //return hash;
    Hasher h = Hashing.adler32().newHasher();
    h.putString(name, Util.UTF);/*from   ww w . j  a  va  2 s  .  co m*/
    h.putObject(data, Util.funnel);
    return h.hash().asInt();
}

From source file:org.apache.metron.hbase.converters.threatintel.ThreatIntelKey.java

@Override
public byte[] toBytes() {
    byte[] indicatorBytes = Bytes.toBytes(indicator);
    Hasher hasher = hFunction.get().newHasher();
    hasher.putBytes(Bytes.toBytes(indicator));
    byte[] prefix = hasher.hash().asBytes();
    byte[] val = new byte[indicatorBytes.length + prefix.length];
    int pos = 0;/* w  w  w .j  ava  2 s  .  c  o m*/
    for (int i = 0; pos < prefix.length; ++pos, ++i) {
        val[pos] = prefix[i];
    }
    for (int i = 0; i < indicatorBytes.length; ++pos, ++i) {
        val[pos] = indicatorBytes[i];
    }
    return val;
}

From source file:com.google.api.control.aggregator.CheckRequestAggregator.java

/**
 * Obtains the {@code HashCode} for the contents of {@code value}.
 *
 * @param value a {@code CheckRequest} to be signed
 * @return the {@code HashCode} corresponding to {@code value}
 *//*from www.j a  v  a 2 s . c o  m*/
public static HashCode sign(CheckRequest value) {
    Hasher h = Hashing.md5().newHasher();
    Operation o = value.getOperation();
    if (o == null || Strings.isNullOrEmpty(o.getConsumerId()) || Strings.isNullOrEmpty(o.getOperationName())) {
        throw new IllegalArgumentException("CheckRequest should have a valid operation");
    }
    h.putString(o.getConsumerId(), StandardCharsets.UTF_8);
    h.putChar('\0');
    h.putString(o.getOperationName(), StandardCharsets.UTF_8);
    h.putChar('\0');
    Signing.putLabels(h, o.getLabels());
    for (MetricValueSet mvSet : o.getMetricValueSetsList()) {
        h.putString(mvSet.getMetricName(), StandardCharsets.UTF_8);
        h.putChar('\0');
        for (MetricValue metricValue : mvSet.getMetricValuesList()) {
            MetricValues.putMetricValue(h, metricValue);
        }
    }
    return h.hash();
}