Java 类com.google.common.hash.Hasher 实例源码

项目:incubator-servicecomb-java-chassis    文件:SignatureUtils.java   
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);
    }
  }

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

  return hasher.hash().toString();
}
项目:BIMplatform    文件:CacheDescriptor.java   
public String getCacheKey() {
    Hasher hasher = hf.newHasher();
    hasher.putUnencodedChars(queryKey);
    for (Number id : ids) {
        if (id instanceof Integer) {
            hasher.putInt(id.intValue());
        } else if (id instanceof Long) {
            hasher.putLong(id.longValue());
        } else if (id instanceof Short) {
            hasher.putLong(id.shortValue());
        } else if (id instanceof Double) {
            hasher.putDouble(id.doubleValue());
        } else if (id instanceof Float) {
            hasher.putFloat(id.floatValue());
        } else if (id instanceof Byte) {
            hasher.putFloat(id.byteValue());
        }
    }
    HashCode hashcode = hasher.hash();
    return hashcode.toString();
}
项目:odoxSync    文件:FileOperations.java   
private void processRegions(File file, RegionProcessor regionProcessor, String randomAccessFileMode, FileChannel.MapMode mapMode) throws IOException {
    try (
            RandomAccessFile randomAccessFile = new RandomAccessFile(file.getAbsolutePath().toFile(), randomAccessFileMode);
            FileChannel channel = randomAccessFile.getChannel()

    ) {
        RegionCalculator.calculateForSize(file, file.getSize());


        for (Region region : file.getRegions().values()) {
            Hasher hasher = Hashing.sha256().newHasher();
            MappedByteBuffer mappedByteBuffer = channel.map(mapMode, region.getOffset(), region.getSize());

            int sum = regionProcessor.processRegion(region, hasher, mappedByteBuffer);

            region.setQuickDigest(sum);

            byte[] slowDigest = hasher.hash().asBytes();
            region.setSlowDigest(slowDigest);

            clientMessageHandler.submitClientRegionMessage(clientId, file, region.getOffset(), region.getSize(), sum, slowDigest);
        }
    }
}
项目:endpoints-management-java    文件: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}
 */
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();
}
项目:endpoints-management-java    文件:QuotaRequestAggregator.java   
@VisibleForTesting
static HashCode sign(AllocateQuotaRequest req) {
  Hasher h = Hashing.md5().newHasher();
  QuotaOperation o = req.getAllocateOperation();
  h.putString(o.getMethodName(), StandardCharsets.UTF_8);
  h.putChar('\0');
  h.putString(o.getConsumerId(), StandardCharsets.UTF_8);
  ImmutableSortedSet.Builder<String> builder =
      new ImmutableSortedSet.Builder<>(Ordering.natural());
  for (MetricValueSet mvSet : o.getQuotaMetricsList()) {
    builder.add(mvSet.getMetricName());
  }
  for (String metricName : builder.build()) {
    h.putChar('\0');
    h.putString(metricName, StandardCharsets.UTF_8);
  }
  return h.hash();
}
项目:sfs    文件:XVersion.java   
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();
    }
}
项目:sfs    文件:XVersion.java   
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();
    }
}
项目:elastic-load-balancing-tools    文件:NullHasherTest.java   
@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);
}
项目:elastic-load-balancing-tools    文件:CRC32CInputStreamTest.java   
private void assertCRC32C(int crc, byte... data) throws IOException {
    byte[] buf = new byte[100];
    assertThat(buf.length, greaterThanOrEqualTo(data.length));

    CRC32CInputStream in = new CRC32CInputStream(new ByteArrayInputStream(data), true);
    assertEquals(data.length, in.read(buf));
    int checksum = in.getChecksum();
    in.close();
    assertEquals("Expected " + Util.toHex(crc)
            + ", calculated " + Util.toHex(checksum), crc, checksum);

    // make sure we calculate the same value as the hasher:
    {
        Hasher hasher = Hashing.crc32c().newHasher();
        hasher.putBytes(data);
        assertEquals("Expected " + Util.toHex(crc)
                + ", calculated " + Util.toHex(hasher.hash().asInt()), crc, hasher.hash().asInt());
    }
}
项目:gradle-jmh-report    文件:ReadChunkingBenchmark.java   
@Benchmark
public void readWholeChunk() throws IOException {
    long consumedBytes = 0;
    byte[] bytes = new byte[4096];
    Hasher hasher = Hashing.crc32().newHasher();
    try (ReadableByteChannel byteChannel = _testFile.open();) {
        while (byteChannel.read(_chunkBuffer) > 0) {
            _chunkBuffer.flip();
            while (_chunkBuffer.hasRemaining()) {
                int length = Math.min(bytes.length, _chunkBuffer.remaining());
                _chunkBuffer.get(bytes, 0, length);
                hasher.putBytes(bytes, 0, length);
                consumedBytes += length;
            }
            _chunkBuffer.rewind();
        }
    }
    assertThat(consumedBytes).isEqualTo(BinaryByteUnit.GIBIBYTES.toBytes(1));
    assertThat(hasher.hash().toString()).isEqualTo("d3a214f6");
}
项目:gradle-jmh-report    文件:SizingReadBenchmark.java   
private String readFile(ByteBuffer byteBuffer, int consumeBytesChunk) throws IOException {
    Hasher hasher = Hashing.crc32().newHasher();
    byte[] bytes = new byte[consumeBytesChunk];
    try (ReadableByteChannel channel = _testFile.open();) {
        while (channel.read(byteBuffer) > 0) {
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
                int length = Math.min(bytes.length, byteBuffer.remaining());
                byteBuffer.get(bytes, 0, length);
                hasher.putBytes(bytes, 0, length);
            }
            byteBuffer.rewind();
        }
    }
    String hash = hasher.hash().toString();
    assertThat(hash).isEqualTo("d3a214f6");
    return hash;
}
项目:beam    文件:PackageUtil.java   
public static PackageAttributes forBytesToStage(
    byte[] bytes, String targetName, String stagingPath) {
  Hasher hasher = Hashing.md5().newHasher();
  String hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.putBytes(bytes).hash().asBytes());
  long size = bytes.length;

  String uniqueName = getUniqueContentName(new File(targetName), hash);

  String resourcePath =
      FileSystems.matchNewResource(stagingPath, true)
          .resolve(uniqueName, StandardResolveOptions.RESOLVE_FILE)
          .toString();
  DataflowPackage target = new DataflowPackage();
  target.setName(uniqueName);
  target.setLocation(resourcePath);

  return new AutoValue_PackageUtil_PackageAttributes(null, bytes, target, size, hash);
}
项目:ElasticJob    文件:RegistryCenterFactory.java   
/**
 * 创建注册中心.
 *
 * @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();
    if (registryCenterMap.containsKey(hashCode)) {
        return registryCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    registryCenterMap.putIfAbsent(hashCode, result);
    return result;
}
项目:dsl-devkit    文件:ScopeResourceDescriptionStrategy.java   
@Override
public boolean createEObjectDescriptions(final EObject eObject, final IAcceptor<IEObjectDescription> acceptor) {
  if (getQualifiedNameProvider() == null || !(eObject instanceof ScopeModel)) {
    return false;
  }
  ScopeModel model = (ScopeModel) eObject;
  try {
    QualifiedName qualifiedName = getQualifiedNameProvider().getFullyQualifiedName(model);
    if (qualifiedName != null) {
      Hasher hasher = Hashing.murmur3_32().newHasher(HASHER_CAPACITY);
      hasher.putUnencodedChars(getSourceText(model));
      for (ScopeModel include : model.getIncludedScopes()) {
        hasher.putUnencodedChars(getSourceText(include));
      }
      acceptor.accept(EObjectDescription.create(qualifiedName, model, Collections.singletonMap("fingerprint", hasher.hash().toString())));
    }
    // CHECKSTYLE:CHECK-OFF IllegalCatch
  } catch (RuntimeException e) {
    // CHECKSTYLE:CHECK-ON
    LOG.error(e.getMessage(), e);
  }
  return false;
}
项目:dsl-devkit    文件:AbstractStreamingFingerprintComputer.java   
/**
 * 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());
  }
}
项目:dsl-devkit    文件:DefaultInferredElementFragmentProvider.java   
/**
 * Computes a hash code for the given {@link #getEClass(EObject) EClass} and {@link #getQualifiedName(EObject) qualified name}.
 *
 * @param eClass
 *          EClass to base hash on, must not be {@code null}
 * @param name
 *          qualified name of inferred model element, can be {@code null}
 * @return hash code, never {@code null}
 */
protected HashCode computeHash(final EClass eClass, final QualifiedName name) {
  byte[] eClassUriBytes = eClassToUriBytesMap.get(eClass);
  if (eClassUriBytes == null) {
    eClassUriBytes = EcoreUtil.getURI(eClass).toString().getBytes(Charsets.UTF_8);
    eClassToUriBytesMap.put(eClass, eClassUriBytes);
  }

  Hasher hasher = hashFunction.newHasher(HASHER_CAPACITY);
  hasher.putBytes(eClassUriBytes);

  if (name != null) {
    hasher.putChar('/');

    for (int j = 0; j < name.getSegmentCount(); j++) {
      hasher.putUnencodedChars(name.getSegment(j)).putChar('.');
    }
  }

  return hasher.hash();
}
项目:elastic-config    文件:RegistryCenterFactory.java   
/**
 * 创建注册中心.
 *
 * @param connectString 注册中心连接字符串
 * @param namespace 注册中心命名空间
 * @param digest 注册中心凭证
 * @return 注册中心对象
 */
public static ElasticConfigRegistryCenter 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();
    if (registryCenterMap.containsKey(hashCode)) {
        return registryCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    ElasticConfigRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    registryCenterMap.putIfAbsent(hashCode, result);
    return result;
}
项目:fim    文件:FileState.java   
public void hashObject(Hasher hasher, boolean millisecondsRemoved) {
    hasher
        .putString("FileState", Charsets.UTF_8)
        .putChar(HASH_FIELD_SEPARATOR)
        .putString(fileName, Charsets.UTF_8)
        .putChar(HASH_FIELD_SEPARATOR)
        .putLong(fileLength);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    fileTime.hashObject(hasher, millisecondsRemoved);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    fileHash.hashObject(hasher);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    if (fileAttributes != null) {
        for (Map.Entry<String, String> entry : fileAttributes.entrySet()) {
            hasher
                .putString(entry.getKey(), Charsets.UTF_8)
                .putChar(':')
                .putChar(':')
                .putString(entry.getValue(), Charsets.UTF_8);
            hasher.putChar(HASH_OBJECT_SEPARATOR);
        }
    }
}
项目:heroic    文件:ExactCardinalityBucket.java   
@Override
public void update(final Map<String, String> key, final Metric d) {
    final Hasher hasher = HASH_FUNCTION.newHasher();

    if (includeKey) {
        for (final String k : KEY_ORDER.sortedCopy(key.keySet())) {
            hasher.putString(k, Charsets.UTF_8).putString(key.get(k), Charsets.UTF_8);
        }
    }

    d.hash(hasher);

    if (seen.add(hasher.hash())) {
        count.incrementAndGet();
    }
}
项目:heroic    文件:Series.java   
private HashCode generateHash() {
    final Hasher hasher = HASH_FUNCTION.newHasher();

    if (key != null) {
        hasher.putString(key, Charsets.UTF_8);
    }

    for (final Map.Entry<String, String> kv : tags.entrySet()) {
        final String k = kv.getKey();
        final String v = kv.getValue();

        if (k != null) {
            hasher.putString(k, Charsets.UTF_8);
        }

        if (v != null) {
            hasher.putString(v, Charsets.UTF_8);
        }
    }

    return hasher.hash();
}
项目:che    文件:ETagResponseFilter.java   
/**
 * 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;
}
项目:presto    文件:TestTimeZoneKey.java   
@Test
public void testZoneKeyData()
        throws Exception
{
    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 is more difficult
    assertEquals(hasher.hash().asLong(), 5498515770239515435L, "zone-index.properties file contents changed!");
}
项目:gerrit    文件:GetRevisionActions.java   
@Override
public String getETag(RevisionResource rsrc) {
  Hasher h = Hashing.murmur3_128().newHasher();
  CurrentUser user = rsrc.getUser();
  try {
    rsrc.getChangeResource().prepareETag(h, user);
    h.putBoolean(Submit.wholeTopicEnabled(config));
    ReviewDb db = dbProvider.get();
    ChangeSet cs = mergeSuperSet.get().completeChangeSet(db, rsrc.getChange(), user);
    for (ChangeData cd : cs.changes()) {
      changeResourceFactory.create(cd.notes(), user).prepareETag(h, user);
    }
    h.putBoolean(cs.furtherHiddenChanges());
  } catch (IOException | OrmException | PermissionBackendException e) {
    throw new OrmRuntimeException(e);
  }
  return h.hash().toString();
}
项目:gerrit    文件:FileContentUtil.java   
private static String randSuffix() {
  // Produce a random suffix that is difficult (or nearly impossible)
  // for an attacker to guess in advance. This reduces the risk that
  // an attacker could upload a *.class file and have us send a ZIP
  // that can be invoked through an applet tag in the victim's browser.
  //
  Hasher h = Hashing.murmur3_128().newHasher();
  byte[] buf = new byte[8];

  NB.encodeInt64(buf, 0, TimeUtil.nowMs());
  h.putBytes(buf);

  rng.nextBytes(buf);
  h.putBytes(buf);

  return h.hash().toString();
}
项目:kylin    文件:FactDistinctColumnsMapper.java   
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[rowkeyColIndex[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 = cuboidsBitSet.length; i < n; i++) {
        long value = 0;
        for (int position = 0; position < cuboidsBitSet[i].length; position++) {
            value += rowHashCodesLong[cuboidsBitSet[i][position]];
        }
        cuboidsHLL[i].addHashDirectly(value);
    }
}
项目:kylin    文件:CalculateStatsFromBaseCuboidMapper.java   
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);
    }
}
项目:kylin    文件:NewCubeSamplingMethodTest.java   
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);
    }
}
项目:sockslib    文件:HashPasswordProtector.java   
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;
}
项目:closure-compiler    文件:RandomNameGenerator.java   
/**
 * Returns the {@code nameIdx}-th short name. This might be a reserved name.
 * A user-requested prefix is not included, but the first returned character
 * is supposed to go at position {@code position} in the final name
 */
private String generateSuffix(int position, int nameIdx) {
  StringBuilder name = new StringBuilder();
  int length = getNameLength(position, nameIdx);
  nameIdx++;
  do {
    nameIdx--;
    String alphabet;
    if (position == 0) {
      alphabet = shuffledFirst;
    } else {
      Hasher hasher = Hashing.murmur3_128().newHasher();
      hasher.putInt(length);
      hasher.putUnencodedChars(name);
      int alphabetIdx = (hasher.hash().asInt() & 0x7fffffff) % NUM_SHUFFLES;
      alphabet = shuffledNonFirst.get(alphabetIdx);
    }
    int alphabetSize = alphabet.length();
    char character = alphabet.charAt(nameIdx % alphabetSize);
    name.append(character);

    nameIdx /= alphabetSize;
    position++;
  } while (nameIdx > 0);
  return name.toString();
}
项目:ameba    文件:DatabaseMigrationResolver.java   
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();
}
项目:bazel    文件:RepositoryCache.java   
/**
 * 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 {
  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);
      }
      numBytesRead = stream.read(byteBuffer);
    }
  }
  return hasher.hash().toString();
}
项目:MHAP    文件:HashUtils.java   
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;
}
项目:GUITester-core    文件:IDGenerator_JFC.java   
@Override
public HashCode getComponentHash(ComponentModel src, HashCode parentHashCode) {
  Hasher hasher = hf.newHasher();
  if (parentHashCode != null)
    hasher.putBytes(parentHashCode.asBytes());

  Map<String, String> srcProperties = src.getProperties();
  hasher.putString(srcProperties.get("title"), Charsets.UTF_8);

  for (String key : structuralPropertyList) {
    String value = srcProperties.get(key);
    if (value != null)
      hasher.putString(value, Charsets.UTF_8);
    else
      hasher.putBoolean(false);
  }
  return hasher.hash();
}
项目:GUITester-core    文件:IDGenerator_JFC.java   
@Override
public HashCode getEventHash(EventModel src) {
  Hasher hasher = hf.newHasher();

  String id = src.getComponentModel().get("id");
  if (id != null)
    hasher.putString(id, Charsets.UTF_8);
  else
    hasher.putBoolean(false);

  hasher.putInt(src.getValueHash());

  String componentIdx = src.getComponentModel().get("componentIndex");
  if (componentIdx != null)
    hasher.putString(componentIdx, Charsets.UTF_8);
  else
    hasher.putBoolean(false);

  return hasher.hash();
}
项目:marmotta    文件:ETagGenerator.java   
public static String getWeakETag(RepositoryConnection conn, Resource resource) throws RepositoryException {
    if (resource == null) return "";

    Hasher hasher = buildHasher();
    hasher.putString(resource.stringValue(), Charset.defaultCharset());
    //FIXME: The order of the statements is not defined -> might result in different hash!
    RepositoryResult<Statement> statements = conn.getStatements(resource, null, null, true);
    try {
        while (statements.hasNext()) {
            Statement statement = statements.next();
            hasher.putString(statement.getPredicate().stringValue(), Charset.defaultCharset());
            hasher.putString(statement.getObject().stringValue(), Charset.defaultCharset());
            //TODO: statement modification date?
        }
    } finally {
        statements.close();
    }
    return hasher.hash().toString();
}
项目:buck-cutom    文件:DefaultJavaLibrary.java   
/**
 * Creates a Hasher containing the ABI keys of the dependencies.
 * @param rulesWithAbiToConsider a sorted set containing the dependencies whose ABI key will be
 *     added to the hasher.
 * @return a Hasher containing the ABI keys of the dependencies.
 */
private Hasher createHasherWithAbiKeyForDeps(SortedSet<HasBuildTarget> rulesWithAbiToConsider) {
  Hasher hasher = Hashing.sha1().newHasher();

  for (HasBuildTarget candidate : rulesWithAbiToConsider) {
    if (candidate == this) {
      continue;
    }

    if (candidate instanceof HasJavaAbi) {
      Sha1HashCode abiKey = ((HasJavaAbi) candidate).getAbiKey();
      hasher.putUnencodedChars(abiKey.getHash());
    } else if (candidate instanceof BuildRule) {
      HashCode hashCode = ((BuildRule) candidate).getRuleKey().getHashCode();
      hasher.putBytes(hashCode.asBytes());
    }
  }

  return hasher;
}
项目:buck    文件:ProjectGenerator.java   
private static Path getDerivedSourcesDirectoryForBuildTarget(
    BuildTarget buildTarget, ProjectFilesystem fs) {
  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 = fs.getBuckPaths().getXcodeDir();
  Path derivedSourcesDir = xcodeDir.resolve("derived-sources").resolve(targetFolderName);

  return derivedSourcesDir;
}
项目:buck    文件:MacroExpander.java   
/**
 * Expand the input given for the this macro to some string, which is intended to be written to a
 * file.
 */
default Arg expandForFile(
    BuildTarget target,
    CellPathResolver cellNames,
    BuildRuleResolver resolver,
    ImmutableList<String> input,
    Object precomputedWork)
    throws MacroException {
  // "prefix" should give a stable name, so that the same delegate with the same input can output
  // the same file. We won't optimise for this case, since it's actually unlikely to happen within
  // a single run, but using a random name would cause 'buck-out' to expand in an uncontrolled
  // manner.
  Hasher hasher = Hashing.sha1().newHasher();
  hasher.putString(getClass().getName(), UTF_8);
  input.forEach(s -> hasher.putString(s, UTF_8));
  return makeExpandToFileArg(
      target,
      hasher.hash().toString(),
      expand(target, cellNames, resolver, input, precomputedWork));
}
项目:buck    文件:DefaultOnDiskBuildInfo.java   
@Override
public void writeOutputHashes(FileHashCache fileHashCache) throws IOException {
  ImmutableSortedSet<Path> pathsForArtifact = getPathsForArtifact();

  // Grab and record the output hashes in the build metadata so that cache hits avoid re-hashing
  // file contents.  Since we use output hashes for input-based rule keys and for detecting
  // non-determinism, we would spend a lot of time re-hashing output paths -- potentially in
  // serialized in a single step. So, do the hashing here to distribute the workload across
  // several threads and cache the results.
  ImmutableSortedMap.Builder<String, String> outputHashes = ImmutableSortedMap.naturalOrder();
  Hasher hasher = Hashing.sha1().newHasher();
  for (Path path : pathsForArtifact) {
    String pathString = path.toString();
    HashCode fileHash = fileHashCache.get(projectFilesystem.resolve(path));
    hasher.putBytes(pathString.getBytes(Charsets.UTF_8));
    hasher.putBytes(fileHash.asBytes());
    outputHashes.put(pathString, fileHash.toString());
  }

  projectFilesystem.writeContentsToPath(
      ObjectMappers.WRITER.writeValueAsString(outputHashes.build()),
      metadataDirectory.resolve(BuildInfo.MetadataKey.RECORDED_PATH_HASHES));

  projectFilesystem.writeContentsToPath(
      hasher.hash().toString(), metadataDirectory.resolve(BuildInfo.MetadataKey.OUTPUT_HASH));
}
项目:buck    文件:PathHashing.java   
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))) {
    StringHashing.hashStringAndLength(hasher, MorePaths.pathWithUnixSeparators(path));
    if (!root.equals(path)) {
      children.add(root.relativize(path));
    }
    hasher.putBytes(fileHashLoader.get(path).asBytes());
  }
  return children.build();
}