当前位置: 首页>>代码示例>>Java>>正文


Java Hasher类代码示例

本文整理汇总了Java中com.google.common.hash.Hasher的典型用法代码示例。如果您正苦于以下问题:Java Hasher类的具体用法?Java Hasher怎么用?Java Hasher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Hasher类属于com.google.common.hash包,在下文中一共展示了Hasher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: genSignature

import com.google.common.hash.Hasher; //导入依赖的package包/类
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();
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:20,代码来源:SignatureUtils.java

示例2: sign

import com.google.common.hash.Hasher; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:cloudendpoints,项目名称:endpoints-management-java,代码行数:28,代码来源:CheckRequestAggregator.java

示例3: getCacheKey

import com.google.common.hash.Hasher; //导入依赖的package包/类
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();
}
 
开发者ID:shenan4321,项目名称:BIMplatform,代码行数:22,代码来源:CacheDescriptor.java

示例4: processRegions

import com.google.common.hash.Hasher; //导入依赖的package包/类
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);
        }
    }
}
 
开发者ID:gaganis,项目名称:odoxSync,代码行数:25,代码来源:FileOperations.java

示例5: sign

import com.google.common.hash.Hasher; //导入依赖的package包/类
@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();
}
 
开发者ID:cloudendpoints,项目名称:endpoints-management-java,代码行数:19,代码来源:QuotaRequestAggregator.java

示例6: calculateMd5

import com.google.common.hash.Hasher; //导入依赖的package包/类
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();
    }
}
 
开发者ID:pitchpoint-solutions,项目名称:sfs,代码行数:17,代码来源:XVersion.java

示例7: calculateSha512

import com.google.common.hash.Hasher; //导入依赖的package包/类
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();
    }
}
 
开发者ID:pitchpoint-solutions,项目名称:sfs,代码行数:17,代码来源:XVersion.java

示例8: testBasics

import com.google.common.hash.Hasher; //导入依赖的package包/类
@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);
}
 
开发者ID:aws,项目名称:elastic-load-balancing-tools,代码行数:19,代码来源:NullHasherTest.java

示例9: assertCRC32C

import com.google.common.hash.Hasher; //导入依赖的package包/类
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());
    }
}
 
开发者ID:aws,项目名称:elastic-load-balancing-tools,代码行数:20,代码来源:CRC32CInputStreamTest.java

示例10: readWholeChunk

import com.google.common.hash.Hasher; //导入依赖的package包/类
@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");
}
 
开发者ID:jzillmann,项目名称:gradle-jmh-report,代码行数:21,代码来源:ReadChunkingBenchmark.java

示例11: readFile

import com.google.common.hash.Hasher; //导入依赖的package包/类
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;
}
 
开发者ID:jzillmann,项目名称:gradle-jmh-report,代码行数:19,代码来源:SizingReadBenchmark.java

示例12: forBytesToStage

import com.google.common.hash.Hasher; //导入依赖的package包/类
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);
}
 
开发者ID:apache,项目名称:beam,代码行数:19,代码来源:PackageUtil.java

示例13: createCoordinatorRegistryCenter

import com.google.common.hash.Hasher; //导入依赖的package包/类
/**
 * 创建注册中心.
 *
 * @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;
}
 
开发者ID:zhoujia123,项目名称:ElasticJob,代码行数:27,代码来源:RegistryCenterFactory.java

示例14: createEObjectDescriptions

import com.google.common.hash.Hasher; //导入依赖的package包/类
@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;
}
 
开发者ID:dsldevkit,项目名称:dsl-devkit,代码行数:24,代码来源:ScopeResourceDescriptionStrategy.java

示例15: fingerprintEObject

import com.google.common.hash.Hasher; //导入依赖的package包/类
/**
 * 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());
  }
}
 
开发者ID:dsldevkit,项目名称:dsl-devkit,代码行数:27,代码来源:AbstractStreamingFingerprintComputer.java


注:本文中的com.google.common.hash.Hasher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。