Java Code Examples for com.google.common.hash.HashFunction#newHasher()

The following examples show how to use com.google.common.hash.HashFunction#newHasher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewCubeSamplingMethodTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void putRowKeyToHLLNew(List<String> row, long[] hashValuesLong, HLLCounter[] cuboidCounters, HashFunction hashFunction) {
    int x = 0;
    for (String field : row) {
        Hasher hc = hashFunction.newHasher();
        byte[] bytes = hc.putString(x + field).hash().asBytes();
        hashValuesLong[x++] = Bytes.toLong(bytes);
    }

    for (int i = 0, n = allCuboidsBitSet.length; i < n; i++) {
        long value = 0;
        for (int position = 0; position < allCuboidsBitSet[i].length; position++) {
            value += hashValuesLong[allCuboidsBitSet[i][position]];
        }
        cuboidCounters[i].addHashDirectly(value);
    }
}
 
Example 2
Source File: HashUtils.java    From MHAP with Apache License 2.0 6 votes vote down vote up
public static long[] computeHashes(String item, int numWords, int seed)
{
	long[] hashes = new long[numWords];

	for (int word = 0; word < numWords; word += 2)
	{
		HashFunction hashFunc = Hashing.murmur3_128(seed + word);
		Hasher hasher = hashFunc.newHasher();
		hasher.putUnencodedChars(item);

		// get the two longs out
		HashCode hc = hasher.hash();
		ByteBuffer bb = ByteBuffer.wrap(hc.asBytes());
		hashes[word] = bb.getLong(0);
		if (word + 1 < numWords)
			hashes[word + 1] = bb.getLong(8);
	}

	return hashes;
}
 
Example 3
Source File: SkipScanFilter.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 4
Source File: OutputModule.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String generateOutputFilename(int postfix) {
  HashFunction hashFunction = Hashing.murmur3_128();

  Hasher hasher = hashFunction.newHasher();
  hasher.putUnencodedChars(outputIdentifier);
  hasher.putInt(postfix);

  return protoTempDir.toString() + File.separator + hasher.hash() + ProtoSuffix;
}
 
Example 5
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 6
Source File: ResultsMerger.java    From splicer with Apache License 2.0 5 votes vote down vote up
public long signatureOf(TsdbResult result) {
	HashFunction hf = Hashing.goodFastHash(64);
	Hasher hasher = hf.newHasher();

	List<String> aggTags = result.getAggregateTags();
	if (aggTags != null) {
		List<String> sortedAggTags = Lists.newArrayList(aggTags);
		Collections.sort(sortedAggTags);
		for (String aggTag: sortedAggTags) {
			hasher.putString(aggTag, Charset.forName("ISO-8859-1"));
		}
	}

	Map<String, String> tags;
	if (result.getTags() != null && (tags = result.getTags().getTags()) != null) {
		List<String> tagTokens = Lists.newArrayList(tags.keySet());
		Collections.sort(tagTokens);
		for (String s: tagTokens) {
			hasher.putString(s, Charset.forName("ISO-8859-1"));
			hasher.putString(tags.get(s), Charset.forName("ISO-8859-1"));
		}
	}

	List<String> tsuids = result.getTsuids();
	if (tsuids != null) {
		List<String> sortedTsUIDs = Lists.newArrayList(tsuids);
		Collections.sort(sortedTsUIDs);
		for (String tsuid: sortedTsUIDs) {
			hasher.putString(tsuid, Charset.forName("ISO-8859-1"));
		}
	}

	return hasher.hash().asLong();
}
 
Example 7
Source File: Launcher.java    From launcher with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static byte[] download(String path, String hash, IntConsumer progress) throws IOException, VerificationException
{
	HashFunction hashFunction = Hashing.sha256();
	Hasher hasher = hashFunction.newHasher();
	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

	URL url = new URL(path);
	HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	conn.setRequestProperty("User-Agent", USER_AGENT);
	conn.getResponseCode();

	InputStream err = conn.getErrorStream();
	if (err != null)
	{
		err.close();
		throw new IOException("Unable to download " + path + " - " + conn.getResponseMessage());
	}

	int downloaded = 0;
	try (InputStream in = conn.getInputStream())
	{
		int i;
		byte[] buffer = new byte[1024 * 1024];
		while ((i = in.read(buffer)) != -1)
		{
			byteArrayOutputStream.write(buffer, 0, i);
			hasher.putBytes(buffer, 0, i);
			downloaded += i;
			progress.accept(downloaded);
		}
	}

	HashCode hashCode = hasher.hash();
	if (!hash.equals(hashCode.toString()))
	{
		throw new VerificationException("Unable to verify resource " + path + " - expected " + hash + " got " + hashCode.toString());
	}

	return byteArrayOutputStream.toByteArray();
}
 
Example 8
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Hashes the contents of this byte source using the given hash function.
 *
 * @throws IOException if an I/O error occurs in the process of reading from this source
 */


public HashCode hash(HashFunction hashFunction) throws IOException {
  Hasher hasher = hashFunction.newHasher();
  copyTo(Funnels.asOutputStream(hasher));
  return hasher.hash();
}
 
Example 9
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Hashes the contents of this byte source using the given hash function.
 *
 * @throws IOException if an I/O error occurs in the process of reading from this source
 */


public HashCode hash(HashFunction hashFunction) throws IOException {
  Hasher hasher = hashFunction.newHasher();
  copyTo(Funnels.asOutputStream(hasher));
  return hasher.hash();
}
 
Example 10
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Hashes the contents of this byte source using the given hash function.
 *
 * @throws IOException if an I/O error occurs in the process of reading from this source
 */


public HashCode hash(HashFunction hashFunction) throws IOException {
  Hasher hasher = hashFunction.newHasher();
  copyTo(Funnels.asOutputStream(hasher));
  return hasher.hash();
}
 
Example 11
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Hashes the contents of this byte source using the given hash function.
 *
 * @throws IOException if an I/O error occurs in the process of reading from this source
 */


public HashCode hash(HashFunction hashFunction) throws IOException {
  Hasher hasher = hashFunction.newHasher();
  copyTo(Funnels.asOutputStream(hasher));
  return hasher.hash();
}
 
Example 12
Source File: Hashes.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the hash of the given stream using the given function.
 */
public static HashCode hash(final HashFunction function, final InputStream input) throws IOException {
  Hasher hasher = function.newHasher();
  OutputStream output = Funnels.asOutputStream(hasher);
  ByteStreams.copy(input, output);
  return hasher.hash();
}
 
Example 13
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 14
Source File: AcheToCdrExporter.java    From ache with Apache License 2.0 5 votes vote down vote up
private String uploadMediaFile(byte[] content, String url) throws IOException {
    HashFunction hf = Hashing.sha256();
    Hasher hasher = hf.newHasher();
    hasher.putBytes(content);
    String host = new URL(url).getHost();
    String hs = reverseDomain(host) + "/" + hasher.hash().toString();
    if (skipUpload == false) {
        this.s3Uploader.upload(hs, content);
        System.out.println("Uploaded object: " + hs);
    } else {
        System.out.println("Created object: " + hs);
    }
    return hs;
}
 
Example 15
Source File: ByteSource.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Hashes the contents of this byte source using the given hash function.
 *
 * @throws IOException if an I/O error occurs in the process of reading from this source
 */
public HashCode hash(HashFunction hashFunction) throws IOException {
  Hasher hasher = hashFunction.newHasher();
  copyTo(Funnels.asOutputStream(hasher));
  return hasher.hash();
}
 
Example 16
Source File: HashOutputStream.java    From spork with Apache License 2.0 4 votes vote down vote up
public HashOutputStream(HashFunction hf) {
    hasher = hf.newHasher();
}
 
Example 17
Source File: TestFieldHasherProcessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
static String computeHash(Field.Type fieldType, Object value, HashType hashType) {
  HashFunction hasher = HashingUtil.getHasher(hashType.getHashType());
  PrimitiveSink sink = hasher.newHasher();
  switch (fieldType) {
    case BOOLEAN:
      sink.putBoolean(new BooleanTypeSupport().convert(value));
      break;
    case CHAR:
      sink.putChar(new CharTypeSupport().convert(value));
      break;
    case BYTE:
      sink.putByte(new ByteTypeSupport().convert(value));
      break;
    case SHORT:
      sink.putShort(new ShortTypeSupport().convert(value));
      break;
    case INTEGER:
      sink.putInt(new IntegerTypeSupport().convert(value));
      break;
    case LONG:
      sink.putLong(new LongTypeSupport().convert(value));
      break;
    case FLOAT:
      sink.putFloat(new FloatTypeSupport().convert(value));
      break;
    case DOUBLE:
      sink.putDouble(new DoubleTypeSupport().convert(value));
      break;
    case DATE:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case TIME:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case DATETIME:
      sink.putLong(new DateTypeSupport().convert(value).getTime());
      break;
    case DECIMAL:
      sink.putString(new StringTypeSupport().convert(value), Charset.defaultCharset());
      break;
    case STRING:
      sink.putString(new StringTypeSupport().convert(value), Charset.defaultCharset());
      break;
    case BYTE_ARRAY:
      sink.putBytes(new ByteArrayTypeSupport().convert(value));
      break;
    case MAP:
    case LIST:
    default:
      return null;
  }

  // this was the problem in SDC-6540.
  sink.putByte((byte)0);

  return ((Hasher)sink).hash().toString();
}
 
Example 18
Source File: ETagResponseFilter.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Filter the given container response
 *
 * @param containerResponse the response to use
 */
public void doFilter(GenericContainerResponse containerResponse) {

  // get entity of the response
  Object entity = containerResponse.getEntity();

  // no entity, skip
  if (entity == null) {
    return;
  }

  // Only handle JSON content
  if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
    return;
  }

  // Get the request
  ApplicationContext applicationContext = ApplicationContext.getCurrent();
  Request request = applicationContext.getRequest();

  // manage only GET requests
  if (!HttpMethod.GET.equals(request.getMethod())) {
    return;
  }

  // calculate hash with MD5
  HashFunction hashFunction = Hashing.md5();
  Hasher hasher = hashFunction.newHasher();
  boolean hashingSuccess = true;

  // Manage a list
  if (entity instanceof List) {
    List<?> entities = (List) entity;
    for (Object simpleEntity : entities) {
      hashingSuccess = addHash(simpleEntity, hasher);
      if (!hashingSuccess) {
        break;
      }
    }
  } else {
    hashingSuccess = addHash(entity, hasher);
  }

  // if we're able to handle the hash
  if (hashingSuccess) {

    // get result of the hash
    HashCode hashCode = hasher.hash();

    // Create the entity tag
    EntityTag entityTag = new EntityTag(hashCode.toString());

    // Check the etag
    Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);

    // not modified ?
    if (builder != null) {
      containerResponse.setResponse(builder.tag(entityTag).build());
    } else {
      // it has been changed, so send response with new ETag and entity
      Response.ResponseBuilder responseBuilder =
          Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
      containerResponse.setResponse(responseBuilder.build());
    }
  }
}
 
Example 19
Source File: Hasher.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
public static Hasher of(HashFunction function) {
	com.google.common.hash.Hasher hasher = function.newHasher();
	return new Hasher(hasher);
}
 
Example 20
Source File: ElementUtils.java    From antiquity with GNU General Public License v3.0 3 votes vote down vote up
/**
 * <p>
 * Calculate the private hash of an {@link Element}.
 * </p>
 * 
 * <p>
 * The private hash contains only the properties of the {@link Element},
 * without its associated elements.
 * </p>
 * 
 * <p>
 * The hash is calculated based on SHA1 algorithm
 * </p>
 * 
 * TODO Handle arrays values properly.
 * 
 * @param element The element to calculate the private hash for.
 * @param excludedKeys the keys to exclude when hash is calculated.
 * @return A string representation of the hash
 * @see HashCode#toString()
 */
public static String calculateElementPrivateHash(Element element, Set<String> excludedKeys) {
    Map<String, Object> props = ElementUtils.getPropertiesAsMap(element, excludedKeys);
    Joiner.MapJoiner propsJoiner = Joiner.on('&').withKeyValueSeparator("=");

    HashFunction hf = Hashing.sha1();
    Hasher h = hf.newHasher();

    //h.putString("[" + element.getId().toString() + "]");
    h.putString(propsJoiner.join(props), Charset.defaultCharset());

    return h.hash().toString();
}