Java Code Examples for com.google.common.hash.Hasher#putString()

The following examples show how to use com.google.common.hash.Hasher#putString() . 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: TestTimeZoneKey.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testZoneKeyData()
{
    Hasher hasher = Hashing.murmur3_128().newHasher();

    SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>()
    {
        @Override
        public int compare(TimeZoneKey left, TimeZoneKey right)
        {
            return Short.compare(left.getKey(), right.getKey());
        }
    }, TimeZoneKey.getTimeZoneKeys());

    for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) {
        hasher.putShort(timeZoneKey.getKey());
        hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8);
    }
    // Zone file should not (normally) be changed, so let's make this more difficult
    assertEquals(hasher.hash().asLong(), -972834036790299986L, "zone-index.properties file contents changed!");
}
 
Example 2
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 3
Source File: DatabaseMigrationResolver.java    From ameba with MIT License 6 votes vote down vote up
static int calculateChecksum(String str) {
    Hasher hasher = Hashing.murmur3_32().newHasher();

    BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
    try {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            hasher.putString(line.trim(), Charsets.UTF_8);
        }
    } catch (IOException e) {
        String message = "Unable to calculate checksum";
        throw new FlywayException(message, e);
    }

    return hasher.hash().asInt();
}
 
Example 4
Source File: ETagResponseFilter.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Helper method to add entity to hash. If there is an invalid entity type it will return false
 *
 * @param entity the entity object to analyze and extract JSON for hashing it
 * @param hasher the hasher used to add the hashes
 */
protected boolean addHash(Object entity, Hasher hasher) {
  // get entity type
  EntityType entityType = getElementType(entity);

  // check
  if (entityType == UNKNOWN) {
    // unknown entity type, cannot perform hash
    return false;
  }
  // add hash if all is OK
  try {
    hasher.putString(getJson(entity, entityType), Charset.defaultCharset());
  } catch (RuntimeException e) {
    return false;
  }
  return true;
}
 
Example 5
Source File: EventTraceDataSourceFactory.java    From shardingsphere-elasticjob-lite with Apache License 2.0 6 votes vote down vote up
/**
 * Create event trace data source.
 * 
 * @param driverClassName database driver class name
 * @param url database URL
 * @param username database username
 * @param password database password
 * @return event trace data source
 */
public static EventTraceDataSource createEventTraceDataSource(final String driverClassName, final String url, final String username, final String password) {
    Hasher hasher = Hashing.md5().newHasher().putString(driverClassName, Charsets.UTF_8).putString(url, Charsets.UTF_8);
    if (!Strings.isNullOrEmpty(username)) {
        hasher.putString(username, Charsets.UTF_8);
    }
    if (null != password) {
        hasher.putString(password, Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    EventTraceDataSource result = DATA_SOURCE_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    EventTraceDataSourceConfiguration eventTraceDataSourceConfiguration = new EventTraceDataSourceConfiguration(driverClassName, url, username);
    if (null != password) {
        eventTraceDataSourceConfiguration.setPassword(password);
    }
    result = new EventTraceDataSource(eventTraceDataSourceConfiguration);
    result.init();
    DATA_SOURCE_REGISTRY.put(hashCode, result);
    return result;
}
 
Example 6
Source File: RegistryCenterFactory.java    From shardingsphere-elasticjob-lite with Apache License 2.0 6 votes vote down vote up
/**
 * Create registry center.
 *
 * @param connectString registry center connect string
 * @param namespace registry center namespace
 * @param digest registry center digest
 * @return registry center
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final String digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace, Charsets.UTF_8);
    if (!Strings.isNullOrEmpty(digest)) {
        hasher.putString(digest, Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (!Strings.isNullOrEmpty(digest)) {
        zkConfig.setDigest(digest);
    }
    result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    REG_CENTER_REGISTRY.put(hashCode, result);
    return result;
}
 
Example 7
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 8
Source File: NullHasherTest.java    From elastic-load-balancing-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasics() {
    Hasher hasher = NullHasher.INSTANCE;
    assertEquals(0, hasher.hash().asInt());
    hasher.putBoolean(false);
    hasher.putByte((byte) 3);
    hasher.putBytes(new byte[0]);
    hasher.putBytes(null, 3, 3);
    hasher.putChar('c');
    hasher.putDouble(3.3);
    hasher.putFloat(3.4f);
    hasher.putInt(7);
    hasher.putLong(3);
    hasher.putObject(null, null);
    hasher.putShort((short) 7);
    hasher.putString(null, null);
    hasher.putUnencodedChars(null);
}
 
Example 9
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 10
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 11
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 12
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 13
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 14
Source File: RegistryCenterFactory.java    From eagle with Apache License 2.0 6 votes vote down vote up
/**
 * 创建注册中心.
 *
 * @param connectString 注册中心连接字符串
 * @param namespace 注册中心命名空间
 * @param digest 注册中心凭证
 * @return 注册中心对象
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace, Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    MergeConfig zkConfig = new MergeConfig();
    zkConfig.addExt(ConfigEnum.address.getName(), connectString);
    zkConfig.addExt(ConfigEnum.namespace.getName(), namespace);
    if (digest.isPresent()) {
        zkConfig.addExt(ConfigEnum.digest.getName(), digest.get());
    }
    result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    REG_CENTER_REGISTRY.put(hashCode, result);
    return result;
}
 
Example 15
Source File: AbstractVersionedTargetGraphBuilder.java    From buck with Apache License 2.0 5 votes vote down vote up
/** @return a flavor to which summarizes the given version selections. */
static Flavor getVersionedFlavor(SortedMap<BuildTarget, Version> versions) {
  Preconditions.checkArgument(!versions.isEmpty());
  Hasher hasher = Hashing.md5().newHasher();
  for (Map.Entry<BuildTarget, Version> ent : versions.entrySet()) {
    hasher.putString(ent.getKey().toString(), Charsets.UTF_8);
    hasher.putString(ent.getValue().getName(), Charsets.UTF_8);
  }
  return InternalFlavor.of("v" + hasher.hash().toString().substring(0, 7));
}
 
Example 16
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 17
Source File: VerifyMultipartRequestsWorkComponentTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
private static String getHashForMultipartPayload(String name, String filename, byte[] payloadBytes) {
    Hasher hasher = hashFunction.newHasher()
                                .putString(name, Charsets.UTF_8);
    if (filename != null)
        hasher = hasher.putString(filename, Charsets.UTF_8);

    hasher = hasher.putBytes(payloadBytes);

    HashCode hc = hasher.hash();
    return hc.toString();
}
 
Example 18
Source File: PreDexedFilesSorter.java    From buck with Apache License 2.0 4 votes vote down vote up
/** @see CanaryFactory#create(String, String) */
private DexWithClasses createCanary(
    ProjectFilesystem filesystem,
    String storeName,
    int index,
    ImmutableList.Builder<Step> steps) {
  String canaryIndex = dexStore.index(groupIndex, index);
  if (!groupIndex.isPresent()) {
    canaryIndex = String.format("%02d", index);
  }
  FileLike fileLike = CanaryFactory.create(storeName, canaryIndex);
  String canaryDirName = String.format("canary_%s_%d", storeName, index);
  Path scratchDirectoryForCanaryClass = canaryDirectory.resolve(canaryDirName);

  // Strip the .class suffix to get the class name for the DexWithClasses object.
  String relativePathToClassFile = fileLike.getRelativePath();
  Preconditions.checkState(relativePathToClassFile.endsWith(".class"));
  String className = relativePathToClassFile.replaceFirst("\\.class$", "");

  // Write out the .class file.
  steps.add(
      new AbstractExecutionStep("write_canary_class") {
        @Override
        public StepExecutionResult execute(ExecutionContext context) throws IOException {
          Path classFile = scratchDirectoryForCanaryClass.resolve(relativePathToClassFile);
          try (InputStream inputStream = fileLike.getInput()) {
            filesystem.createParentDirs(classFile);
            filesystem.copyToPath(inputStream, classFile);
          }
          return StepExecutionResults.SUCCESS;
        }
      });

  return new DexWithClasses() {
    @Override
    public int getWeightEstimate() {
      // Because we do not know the units being used for DEX size estimation and the canary
      // should be very small, assume the size is zero.
      return 0;
    }

    @Nullable
    @Override
    public BuildTarget getSourceBuildTarget() {
      return null;
    }

    @Override
    public SourcePath getSourcePathToDexFile() {
      return PathSourcePath.of(filesystem, scratchDirectoryForCanaryClass);
    }

    @Override
    public ImmutableSet<String> getClassNames() {
      return ImmutableSet.of(className);
    }

    @Override
    public Sha1HashCode getClassesHash() {
      // The only thing unique to canary classes is the index,
      // which is captured by canaryDirName.
      Hasher hasher = Hashing.sha1().newHasher();
      hasher.putString(canaryDirName, Charsets.UTF_8);
      return Sha1HashCode.fromHashCode(hasher.hash());
    }
  };
}
 
Example 19
Source File: ShardGroupCompactionImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Hash our data into a consistent long
 */
protected Hasher doHash( final ApplicationScope scope, final DirectedEdgeMeta directedEdgeMeta,
                         final ShardEntryGroup shardEntryGroup ) {

    final Hasher hasher = MURMUR_128.newHasher();


    addToHash( hasher, scope.getApplication() );


    for ( DirectedEdgeMeta.NodeMeta nodeMeta : directedEdgeMeta.getNodes() ) {
        addToHash( hasher, nodeMeta.getId() );
        hasher.putInt( nodeMeta.getNodeType().getStorageValue() );
    }


    /**
     * Add our edge type
     */
    for ( String type : directedEdgeMeta.getTypes() ) {
        hasher.putString( type, CHARSET );
    }


    return 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();
}