com.google.common.hash.Hasher Java Examples

The following examples show how to use com.google.common.hash.Hasher. 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: SignatureUtils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static String genSignature(HttpServletRequestEx requestEx) {
  Hasher hasher = Hashing.sha256().newHasher();
  hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8);
  for (String paramName : paramNames) {
    String paramValue = requestEx.getHeader(paramName);
    if (paramValue != null) {
      hasher.putString(paramName, StandardCharsets.UTF_8);
      hasher.putString(paramValue, StandardCharsets.UTF_8);
      System.out.printf("%s %s\n", paramName, paramValue);
    }
  }

  if (!StringUtils.startsWithIgnoreCase(requestEx.getContentType(), MediaType.APPLICATION_FORM_URLENCODED)) {
    byte[] bytes = requestEx.getBodyBytes();
    if (bytes != null) {
      hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength());
    }
  }

  return hasher.hash().toString();
}
 
Example #2
Source File: PathHashingTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void sameContentsSameNameHaveSameHash() throws IOException {
  SettableFakeClock clock = new SettableFakeClock(1000, 0);
  FakeProjectFilesystem filesystem1 = new FakeProjectFilesystem(clock);
  filesystem1.touch(Paths.get("foo/bar.txt"));

  FakeProjectFilesystem filesystem2 = new FakeProjectFilesystem(clock);
  filesystem2.touch(Paths.get("foo/bar.txt"));

  Hasher hasher1 = Hashing.sha1().newHasher();
  PathHashing.hashPath(hasher1, fileHashCache, filesystem1, Paths.get("foo"));

  Hasher hasher2 = Hashing.sha1().newHasher();
  PathHashing.hashPath(hasher2, fileHashCache, filesystem2, Paths.get("foo"));

  assertThat(hasher1.hash(), equalTo(hasher2.hash()));
}
 
Example #3
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 #4
Source File: CalculateStatsFromBaseCuboidMapper.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void putRowKeyToHLLNew(String[] row) {
    //generate hash for each row key column
    for (int i = 0; i < nRowKey; i++) {
        Hasher hc = hf.newHasher();
        String colValue = row[i];
        if (colValue == null)
            colValue = "0";
        byte[] bytes = hc.putString(colValue).hash().asBytes();
        rowHashCodesLong[i] = (Bytes.toLong(bytes) + i);//add column ordinal to the hash value to distinguish between (a,b) and (b,a)
    }

    // user the row key column hash to get a consolidated hash for each cuboid
    for (int i = 0, n = allCuboidsBitSet.length; i < n; i++) {
        long value = 0;
        for (int position = 0; position < allCuboidsBitSet[i].length; position++) {
            value += rowHashCodesLong[allCuboidsBitSet[i][position]];
        }
        allCuboidsHLL[i].addHashDirectly(value);
    }
}
 
Example #5
Source File: CellUtils.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
@Override
public int hashCode() {
    Hasher hasher = Hashing.goodFastHash(MIN_BITS).newHasher();
    hasher.putBytes(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
    hasher.putBytes(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
    int qualifierLength = cell.getQualifierLength();
    int qualifierOffset = cell.getQualifierOffset();
    if (isShadowCell()) {
        qualifierLength = qualifierLengthFromShadowCellQualifier(cell.getQualifierArray(),
                cell.getQualifierOffset(),
                cell.getQualifierLength());
        if (startsWith(cell.getQualifierArray(), cell.getQualifierOffset(),
                cell.getQualifierLength(), SHADOW_CELL_PREFIX)) {
            qualifierOffset = qualifierOffset + SHADOW_CELL_PREFIX.length;
        }
    }

    hasher.putBytes(cell.getQualifierArray(),qualifierOffset , qualifierLength);
    hasher.putLong(cell.getTimestamp());
    return hasher.hash().asInt();
}
 
Example #6
Source File: ServiceInfo.java    From usergrid with Apache License 2.0 6 votes vote down vote up
public ServiceInfo( String name, boolean rootService, String rootType, String containerType, String collectionName,
                    String itemType, List<String> patterns, List<String> collections ) {
    this.name = name;
    this.rootService = rootService;
    this.rootType = rootType;
    this.containerType = containerType;
    this.collectionName = collectionName;
    this.itemType = itemType;
    this.patterns = patterns;
    this.collections = collections;

    Hasher hasher = Hashing.md5().newHasher();

    for ( String pattern : patterns ) {
        hasher.putString( pattern, UTF_8 );
    }

    hashCode = hasher.hash().asInt();
}
 
Example #7
Source File: Config.java    From buck with Apache License 2.0 6 votes vote down vote up
private HashCode computeOrderIndependentHashCode() {
  ImmutableMap<String, ImmutableMap<String, String>> rawValues = rawConfig.getValues();
  ImmutableSortedMap.Builder<String, ImmutableSortedMap<String, String>> expanded =
      ImmutableSortedMap.naturalOrder();
  for (String section : rawValues.keySet()) {
    expanded.put(section, ImmutableSortedMap.copyOf(get(section)));
  }

  ImmutableSortedMap<String, ImmutableSortedMap<String, String>> sortedConfigMap =
      expanded.build();

  Hasher hasher = Hashing.sha256().newHasher();
  for (Entry<String, ImmutableSortedMap<String, String>> entry : sortedConfigMap.entrySet()) {
    hasher.putString(entry.getKey(), StandardCharsets.UTF_8);
    for (Entry<String, String> nestedEntry : entry.getValue().entrySet()) {
      hasher.putString(nestedEntry.getKey(), StandardCharsets.UTF_8);
      hasher.putString(nestedEntry.getValue(), StandardCharsets.UTF_8);
    }
  }

  return hasher.hash();
}
 
Example #8
Source File: AbstractStreamingFingerprintComputer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generate a fingerprint for the target object using its URI.
 *
 * @param target
 *          The target object
 * @param context
 *          The object containing the reference
 * @param hasher
 *          hasher to stream to
 */
private void fingerprintEObject(final EObject target, final EObject context, final Hasher hasher) {
  if (target == null) {
    hasher.putUnencodedChars(NULL_STRING);
  } else if (target.eIsProxy()) {
    if (context.eResource() instanceof LazyLinkingResource) {
      final URI proxyUri = ((InternalEObject) target).eProxyURI();
      if (!((LazyLinkingResource) context.eResource()).getEncoder().isCrossLinkFragment(context.eResource(), proxyUri.fragment())) {
        hasher.putUnencodedChars(proxyUri.toString());
        return;
      }
    }
    hasher.putUnencodedChars(UNRESOLVED_STRING);
  } else {
    hasher.putUnencodedChars(EcoreUtil.getURI(target).toString());
  }
}
 
Example #9
Source File: Utils.java    From buck with Apache License 2.0 6 votes vote down vote up
/** Generate a cell relative buck-out path for derived sources for the {@code buildTarget}. */
static Path getDerivedSourcesDirectoryForBuildTarget(
    BuildTarget buildTarget, ProjectFilesystem projectFilesystem) {
  String fullTargetName = buildTarget.getFullyQualifiedName();
  byte[] utf8Bytes = fullTargetName.getBytes(Charset.forName("UTF-8"));

  Hasher hasher = Hashing.sha1().newHasher();
  hasher.putBytes(utf8Bytes);

  String targetSha1Hash = hasher.hash().toString();
  String targetFolderName = buildTarget.getShortName() + "-" + targetSha1Hash;

  Path xcodeDir = projectFilesystem.getBuckPaths().getXcodeDir();
  Path derivedSourcesDir = xcodeDir.resolve("derived-sources").resolve(targetFolderName);

  return derivedSourcesDir;
}
 
Example #10
Source File: PathHashingTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void sameNameDifferentContentsHaveDifferentHashes() throws IOException {
  SettableFakeClock clock = new SettableFakeClock(1000, 0);
  FakeProjectFilesystem filesystem1 = new FakeProjectFilesystem(clock);
  filesystem1.touch(Paths.get("foo/bar.txt"));

  FakeProjectFilesystem filesystem2 = new FakeProjectFilesystem(clock);
  filesystem2.touch(Paths.get("foo/bar.txt"));

  Hasher hasher1 = Hashing.sha1().newHasher();
  PathHashing.hashPath(hasher1, fileHashCache, filesystem1, Paths.get("foo"));

  Hasher hasher2 = Hashing.sha1().newHasher();
  PathHashing.hashPath(hasher2, modifiedFileHashCache, filesystem2, Paths.get("foo"));

  assertThat(hasher1.hash(), not(equalTo(hasher2.hash())));
}
 
Example #11
Source File: KeyBufferTest.java    From HaloDB with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "hashes", dependsOnMethods = "testHashFinish")
public void testHashFinish16(HashAlgorithm hashAlgorithm) throws Exception
{

    byte[] ref = TestUtils.generateRandomByteArray(14);
    ByteBuffer buf = ByteBuffer.allocate(16);
    buf.put((byte) (42 & 0xff));
    buf.put(ref);
    buf.put((byte) (0xf0 & 0xff));
    KeyBuffer out = new KeyBuffer(buf.array());
    out.finish(com.oath.halodb.Hasher.create(hashAlgorithm));

    Hasher hasher = hasher(hashAlgorithm);
    hasher.putByte((byte) 42);
    hasher.putBytes(ref);
    hasher.putByte((byte) 0xf0);
    long longHash = hash(hasher);

    assertEquals(out.hash(), longHash);
}
 
Example #12
Source File: XVersion.java    From sfs with Apache License 2.0 6 votes vote down vote up
public Optional<byte[]> calculateMd5() {
    Hasher hasher = md5().newHasher();
    int size = segments.size();
    if (segments.isEmpty() && contentLength != null && contentLength <= 0) {
        return of(EMPTY_MD5);
    } else if (size == 1) {
        return segments.first().getReadMd5();
    } else if (size >= 2) {
        for (TransientSegment transientSegment : segments) {
            hasher.putBytes(transientSegment.getReadMd5().get());
        }
        return of(hasher.hash().asBytes());
    } else {
        return absent();
    }
}
 
Example #13
Source File: PathHashing.java    From buck with Apache License 2.0 6 votes vote down vote up
public static ImmutableSet<Path> hashPath(
    Hasher hasher,
    ProjectFileHashLoader fileHashLoader,
    ProjectFilesystem projectFilesystem,
    Path root)
    throws IOException {
  Preconditions.checkArgument(
      !root.equals(EMPTY_PATH), "Path to hash (%s) must not be empty", root);
  ImmutableSet.Builder<Path> children = ImmutableSet.builder();
  for (Path path : ImmutableSortedSet.copyOf(projectFilesystem.getFilesUnderPath(root))) {
    FastPaths.hashPathFast(hasher, path);
    if (!root.equals(path)) {
      children.add(root.relativize(path));
    }
    hasher.putBytes(fileHashLoader.get(path).asBytes());
  }
  return children.build();
}
 
Example #14
Source File: EdgeSignatureResponseFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
  if (invocation == null) {
    return;
  }

  EncryptContext encryptContext = (EncryptContext) invocation.getHandlerContext().get(EdgeConst.ENCRYPT_CONTEXT);
  if (encryptContext == null) {
    return;
  }
  Hcr hcr = encryptContext.getHcr();

  // bad practice: it's better to set signature in response header
  Buffer bodyBuffer = responseEx.getBodyBuffer();
  String body = bodyBuffer.toString();
  if (body.endsWith("}")) {
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(hcr.getSignatureKey(), StandardCharsets.UTF_8);
    hasher.putString(body, StandardCharsets.UTF_8);
    String signature = hasher.hash().toString();
    LOGGER.info("beforeSendResponse signature: {}", signature);

    body = body.substring(0, body.length() - 1) + ",\"signature\":\"" + signature + "\"}";
    responseEx.setBodyBuffer(Buffer.buffer(body));
  }
}
 
Example #15
Source File: EdgeSignatureResponseFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
  if (invocation == null) {
    return;
  }

  EncryptContext encryptContext = (EncryptContext) invocation.getHandlerContext().get(EdgeConst.ENCRYPT_CONTEXT);
  if (encryptContext == null) {
    return;
  }
  Hcr hcr = encryptContext.getHcr();

  // bad practice: it's better to set signature in response header
  Buffer bodyBuffer = responseEx.getBodyBuffer();
  String body = bodyBuffer.toString();
  if (body.endsWith("}")) {
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(hcr.getSignatureKey(), StandardCharsets.UTF_8);
    hasher.putString(body, StandardCharsets.UTF_8);
    String signature = hasher.hash().toString();
    LOGGER.info("beforeSendResponse signature: {}", signature);

    body = body.substring(0, body.length() - 1) + ",\"signature\":\"" + signature + "\"}";
    responseEx.setBodyBuffer(Buffer.buffer(body));
  }
}
 
Example #16
Source File: FastPathsTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testHashFast() {
  Assume.assumeTrue(
      "BuckUnixPath isn't used on Windows and so the behavior isn't quite the same as Path.",
      Platform.detect() != Platform.WINDOWS);

  String pathString = "some/path/one/two/three";
  BuckUnixPath unixPath = BuckUnixPathUtils.createPath(pathString);
  Hasher buckHasher = Hashing.sha1().newHasher();
  FastPaths.hashPathFast(buckHasher, unixPath);

  Path javaPath = BuckUnixPathUtils.createJavaPath(pathString);
  Hasher javaHasher = Hashing.sha1().newHasher();
  FastPaths.hashPathFast(javaHasher, javaPath);

  assertEquals(javaHasher.hash(), buckHasher.hash());
}
 
Example #17
Source File: HashedFileContent.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/** Create a fingerprint of the given file at the given location. */
public HashedFileContent(URI uri, File file) throws IOException {
	this.uri = uri;
	String ext = uri.fileExtension();
	if (ext == null || "ts".equals(ext) || "js".equals(ext) || "jsx".equals(ext) || "map".equals(ext)
			|| "md".equals(ext)
			|| "hbs".equals(ext)
			|| "json".equals(ext) && !"package.json".equals(uri.lastSegment())) {
		this.hash = file.length();
	} else {
		try (InputStream s = new FileInputStream(file)) {
			Hasher hasher = hashFunction.newHasher();
			s.transferTo(Funnels.asOutputStream(hasher));
			this.hash = hasher.hash().asLong();
		}
	}
}
 
Example #18
Source File: HashPasswordProtector.java    From sockslib with Apache License 2.0 6 votes vote down vote up
private Hasher chooseHasher(HashAlgorithm algorithm) {
  Hasher hasher = null;
  switch (algorithm) {

    case MD5:
      hasher = Hashing.md5().newHasher();
      break;
    case SHA1:
      hasher = Hashing.sha1().newHasher();
      break;
    case SHA256:
      hasher = Hashing.sha256().newHasher();
      break;
    case SHA512:
      hasher = Hashing.sha512().newHasher();
      break;

  }
  return hasher;
}
 
Example #19
Source File: EdgeSignatureRequestFilter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
  EncryptContext encryptContext = (EncryptContext) invocation.getHandlerContext().get(EdgeConst.ENCRYPT_CONTEXT);
  if (encryptContext == null) {
    return null;
  }
  Hcr hcr = encryptContext.getHcr();

  // signature for query and form
  List<String> names = Collections.list(requestEx.getParameterNames());
  names.sort(Comparator.naturalOrder());

  Hasher hasher = Hashing.sha256().newHasher();
  hasher.putString(hcr.getSignatureKey(), StandardCharsets.UTF_8);
  for (String name : names) {
    hasher.putString(name, StandardCharsets.UTF_8);
    hasher.putString(requestEx.getParameter(name), StandardCharsets.UTF_8);
  }
  LOGGER.info("afterReceiveRequest signature: {}", hasher.hash().toString());

  return null;
}
 
Example #20
Source File: XVersion.java    From sfs with Apache License 2.0 6 votes vote down vote up
public Optional<byte[]> calculateSha512() {
    Hasher hasher = sha512().newHasher();
    int size = segments.size();
    if (segments.isEmpty() && contentLength != null && contentLength <= 0) {
        return of(EMPTY_SHA512);
    } else if (size == 1) {
        return segments.first().getReadSha512();
    } else if (size >= 2) {
        for (TransientSegment transientSegment : segments) {
            hasher.putBytes(transientSegment.getReadSha512().get());
        }
        return of(hasher.hash().asBytes());
    } else {
        return absent();
    }
}
 
Example #21
Source File: RepositoryCache.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain the checksum of a file.
 *
 * @param keyType The type of hash function. e.g. SHA-1, SHA-256.
 * @param path The path to the file.
 * @throws IOException
 */
public static String getChecksum(KeyType keyType, Path path)
    throws IOException, InterruptedException {
  Hasher hasher = keyType.newHasher();
  byte[] byteBuffer = new byte[BUFFER_SIZE];
  try (InputStream stream = path.getInputStream()) {
    int numBytesRead = stream.read(byteBuffer);
    while (numBytesRead != -1) {
      if (numBytesRead != 0) {
        // If more than 0 bytes were read, add them to the hash.
        hasher.putBytes(byteBuffer, 0, numBytesRead);
      }
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      numBytesRead = stream.read(byteBuffer);
    }
  }
  return hasher.hash().toString();
}
 
Example #22
Source File: ModuleWithExternalDependenciesTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private static String hash(String... content) {
  Hasher hasher = Hashing.murmur3_128().newHasher();
  for (String data : content) {
    hasher.putUnencodedChars(data);
  }
  return hasher.hash().toString();
}
 
Example #23
Source File: Example.java    From oryx with Apache License 2.0 5 votes vote down vote up
public Example(Feature target, Feature... features) {
  Preconditions.checkArgument(features != null);
  this.features = features;
  this.target = target;
  Hasher hasher = HASH.newHasher();
  for (Feature feature : features) {
    if (feature != null) {
      hasher.putInt(feature.hashCode());
    }
  }
  if (target != null) {
    hasher.putInt(target.hashCode());
  }
  cachedHashCode = hasher.hashCode();
}
 
Example #24
Source File: BinaryParser.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
public Hasher init(final URI url) {
	final Hasher hasher = hashFunction.newHasher();
	if (url != null) {
		// Note that we need to go directly to the hasher to encode explicit IP addresses
		hasher.putUnencodedChars(url.getHost());
		hasher.putByte((byte)0);
	}
	return hasher;
}
 
Example #25
Source File: DefaultBuckModuleManager.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public String load(String pluginId) {
  Hasher hasher = Hashing.murmur3_128().newHasher();
  PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId);
  pluginWrapper
      .getDescriptor()
      .getDependencies()
      .forEach(
          pluginDependency ->
              hasher.putUnencodedChars(
                  moduleHashCache.getUnchecked(pluginDependency.getPluginId())));
  hasher.putUnencodedChars(getBuckModuleJarHash(pluginId));
  return hasher.hash().toString();
}
 
Example #26
Source File: SignatureUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static String genSignature(HttpServletResponseEx responseEx) {
  Hasher hasher = Hashing.sha256().newHasher();
  byte[] bytes = responseEx.getBodyBytes();
  if (bytes != null) {
    hasher.putBytes(bytes, 0, responseEx.getBodyBytesLength());
  }

  return hasher.hash().toString();
}
 
Example #27
Source File: RegistryCenterFactory.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * 创建注册中心.
 *
 * @param zookeeperProperties the zookeeper properties
 *
 * @return 注册中心对象 coordinator registry center
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(ZookeeperProperties zookeeperProperties) {
	Hasher hasher = Hashing.md5().newHasher().putString(zookeeperProperties.getZkAddressList(), Charsets.UTF_8);
	HashCode hashCode = hasher.hash();
	CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
	if (null != result) {
		return result;
	}
	result = new ZookeeperRegistryCenter(zookeeperProperties);
	result.init();
	REG_CENTER_REGISTRY.put(hashCode, result);
	return result;
}
 
Example #28
Source File: KeyBufferTest.java    From HaloDB with Apache License 2.0 5 votes vote down vote up
private Hasher hasher(HashAlgorithm hashAlgorithm)
{
    switch (hashAlgorithm)
    {
        case MURMUR3:
            return Hashing.murmur3_128().newHasher();
        case CRC32:
            return Hashing.crc32().newHasher();
        default:
            throw new IllegalArgumentException();
    }
}
 
Example #29
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 #30
Source File: DefaultJarFileStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private static Function<File, String> hashWith(Supplier<HashFunction> algorithm) {
    return (file) -> {
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            Hasher hasher = algorithm.get().newHasher();
            int b;
            while ((b = inputStream.read()) != -1) {
                hasher.putByte((byte) b);
            }
            return hasher.hash().toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}