本文整理汇总了Java中com.google.common.hash.Hasher.putBytes方法的典型用法代码示例。如果您正苦于以下问题:Java Hasher.putBytes方法的具体用法?Java Hasher.putBytes怎么用?Java Hasher.putBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.hash.Hasher
的用法示例。
在下文中一共展示了Hasher.putBytes方法的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: 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();
}
}
示例3: 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();
}
}
示例4: 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);
}
示例5: 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());
}
}
示例6: 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");
}
示例7: 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;
}
示例8: computeHash
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
/**
* 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();
}
示例9: randSuffix
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
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();
}
示例10: getChecksum
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
/**
* 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();
}
示例11: getComponentHash
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
@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();
}
示例12: createHasherWithAbiKeyForDeps
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
/**
* 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;
}
示例13: getDerivedSourcesDirectoryForBuildTarget
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
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;
}
示例14: writeOutputHashes
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
@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));
}
示例15: hashPath
import com.google.common.hash.Hasher; //导入方法依赖的package包/类
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();
}