本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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);
}
}
}
示例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();
}
示例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();
}
}
示例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();
}
}
示例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);
}
示例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());
}
}
示例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");
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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());
}
}