本文整理了Java中com.google.common.hash.HashCode
类的一些代码示例,展示了HashCode
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashCode
类的具体详情如下:
包路径:com.google.common.hash.HashCode
类名称:HashCode
[英]An immutable hash code of arbitrary bit length.
[中]任意位长度的不可变哈希码。
代码示例来源:origin: google/guava
public void testRoundTripHashCodeUsingBaseEncoding() {
HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
HashCode hash2 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(hash1.toString()));
assertEquals(hash1, hash2);
}
代码示例来源:origin: jooby-project/jooby
private String sha1(final File dir, final File sprite, final File css) throws IOException {
try (Stream<Path> stream = Files.walk(dir.toPath())) {
Hasher sha1 = Hashing.sha1().newHasher();
stream.filter(p -> !Files.isDirectory(p))
.forEach(p -> Try.run(() -> sha1.putBytes(Files.readAllBytes(p))));
if (sprite.exists()) {
sha1.putBytes(Files.readAllBytes(sprite.toPath()));
}
if (css.exists()) {
sha1.putBytes(Files.readAllBytes(css.toPath()));
}
return BaseEncoding.base16().encode(sha1.hash().asBytes()).toLowerCase();
}
}
代码示例来源:origin: google/guava
public void testGetBytesInternal_noCloneOccurs() {
byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00};
HashCode hashCode = HashCode.fromBytes(bytes);
assertEquals(0x0000abcd, hashCode.asInt());
assertEquals("cdab0000", hashCode.toString());
hashCode.getBytesInternal()[0] = (byte) 0x00;
assertEquals(0x0000ab00, hashCode.asInt());
assertEquals("00ab0000", hashCode.toString());
}
代码示例来源:origin: google/guava
/**
* Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined (so, for
* example, you can safely put {@code HashCode} instances into a {@code HashSet}) but is otherwise
* probably not what you want to use.
*/
@Override
public final int hashCode() {
// If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is
// already a (presumably) high-quality hash code, any four bytes of it will do.
if (bits() >= 32) {
return asInt();
}
// If we have less than 4 bytes, use them all.
byte[] bytes = getBytesInternal();
int val = (bytes[0] & 0xFF);
for (int i = 1; i < bytes.length; i++) {
val |= ((bytes[i] & 0xFF) << (i * 8));
}
return val;
}
代码示例来源:origin: google/guava
public void testLongWriteBytesTo() {
byte[] dest = new byte[8];
HashCode.fromLong(42).writeBytesTo(dest, 0, 8);
assertTrue(Arrays.equals(HashCode.fromLong(42).asBytes(), dest));
}
代码示例来源:origin: google/guava
public void testIntWriteBytesTo() {
byte[] dest = new byte[4];
HashCode.fromInt(42).writeBytesTo(dest, 0, 4);
assertTrue(Arrays.equals(HashCode.fromInt(42).asBytes(), dest));
}
代码示例来源:origin: jclouds/legacy-jclouds
public Void call() throws Exception {
HttpWire wire = setUp();
InputStream in = wire.input(fromServer);
ByteArrayOutputStream out = new ByteArrayOutputStream();// TODO
copy(in, out);
byte[] compare = md5().hashBytes(out.toByteArray()).asBytes();
Thread.sleep(100);
assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484);
return null;
}
}
代码示例来源:origin: io.airlift/slice
@Test(invocationCount = 100)
public void testLessThan16Bytes()
throws Exception
{
byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(16));
HashCode expected = Hashing.murmur3_128().hashBytes(data);
Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data));
assertEquals(actual.getBytes(), expected.asBytes());
}
代码示例来源:origin: jclouds/legacy-jclouds
private void checkLoginAsTheNewUser(String expectedUsername) {
LoginResponse response = globalAdminClient.getSessionClient().loginUserInDomainWithHashOfPassword(
expectedUsername, "", base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes()));
assertNotNull(response);
assertNotNull(response.getSessionKey());
assertNotNull(response.getJSessionId());
client.getSessionClient().logoutUser(response.getSessionKey());
}
}
代码示例来源:origin: googleapis/google-cloud-java
private static void setEncryptionHeaders(
HttpHeaders headers, String headerPrefix, Map<Option, ?> options) {
String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
if (key != null) {
BaseEncoding base64 = BaseEncoding.base64();
HashFunction hashFunction = Hashing.sha256();
headers.set(headerPrefix + "algorithm", "AES256");
headers.set(headerPrefix + "key", key);
headers.set(
headerPrefix + "key-sha256",
base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
}
}
代码示例来源:origin: apache/incubator-druid
final String newCommitMetadataSha1 = BaseEncoding.base16().encode(
Hashing.sha1().hashBytes(newCommitMetadataBytes).asBytes()
);
代码示例来源:origin: jclouds/legacy-jclouds
private void assertValidMd5(final InputStream input) throws IOException {
assertEquals(base64().encode(new ByteSource() {
@Override
public InputStream openStream() {
return input;
}
}.hash(md5()).asBytes()), md5);
}
代码示例来源:origin: airlift/slice
@Test(invocationCount = 100)
public void testLessThan4Bytes()
throws Exception
{
byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(4));
int expected = Hashing.murmur3_32().hashBytes(data).asInt();
int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data));
assertEquals(actual, expected);
}
代码示例来源:origin: prestodb/presto
@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(), -4582158485614614451L, "zone-index.properties file contents changed!");
}
代码示例来源:origin: runelite/runelite
@Override
public void run()
{
byte[] hash = Hashing.sha256().hashBytes(data).asBytes();
String hashStr = BaseEncoding.base16().encode(hash);
archive.setHash(hash);
String path = new StringBuilder()
.append(hashStr.substring(0, 2))
.append('/')
.append(hashStr.substring(2))
.toString();
try
{
try (InputStream in = minioClient.getObject(minioBucket, path))
{
return; // already exists
}
catch (ErrorResponseException ex)
{
// doesn't exist
}
minioClient.putObject(minioBucket, path, new ByteArrayInputStream(data), data.length, "binary/octet-stream");
}
catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidArgumentException | InvalidBucketNameException | NoResponseException | IOException | InvalidKeyException | NoSuchAlgorithmException | XmlPullParserException ex)
{
logger.warn("unable to upload data to store", ex);
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Override
public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) {
content = firstNonNull(content, EMPTY_BYTE_ARRAY);
BlobInfo updatedInfo =
blobInfo
.toBuilder()
.setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(content).asBytes()))
.setCrc32c(
BaseEncoding.base64()
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt())))
.build();
return internalCreate(updatedInfo, content, options);
}
代码示例来源:origin: jclouds/legacy-jclouds
@Test
public void testMD5PayloadEnclosing() throws IOException {
Payload payload = Payloads.newPayload("foo");
HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build();
assertEquals(fn.apply(payloadEnclosing), md5().hashString("foo", UTF_8).asBytes());
}
代码示例来源:origin: jooby-project/jooby
private String sha1(final CharSequence source) {
return BaseEncoding.base16()
.encode(Hashing
.sha1()
.hashString(source, charset)
.asBytes())
.substring(0, 8).toLowerCase();
}
代码示例来源:origin: apache/jclouds
@Test(dependsOnMethods = "uploadMultipartBlob", singleThreaded = true)
public void downloadParallelBlob() throws IOException {
final File downloadedFile = new File(bigFile + ".downloaded");
blobStore.downloadBlob(CONTAINER, bigFile.getName(), downloadedFile, executor);
String eTag = Files.hash(downloadedFile, Hashing.md5()).toString();
assertEquals(eTag, etag);
}
代码示例来源:origin: google/guava
public void testPutAfterHash() {
Hasher sha1 = Hashing.sha1().newHasher();
assertEquals(
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
.hash()
.toString());
try {
sha1.putInt(42);
fail();
} catch (IllegalStateException expected) {
}
}
内容来源于网络,如有侵权,请联系作者删除!