com.google.common.hash.HashCode.fromBytes()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(246)

本文整理了Java中com.google.common.hash.HashCode.fromBytes()方法的一些代码示例,展示了HashCode.fromBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashCode.fromBytes()方法的具体详情如下:
包路径:com.google.common.hash.HashCode
类名称:HashCode
方法名:fromBytes

HashCode.fromBytes介绍

[英]Creates a HashCode from a byte array. The array is defensively copied to preserve the immutability contract of HashCode. The array cannot be empty.
[中]从字节数组创建哈希代码。数组被防御性地复制以保留哈希代码的不变性契约。数组不能为空。

代码示例

代码示例来源:origin: google/guava

@Override
 public HashCode hashBytes(byte[] input, int off, int len) {
  return HashCode.fromBytes(Arrays.copyOfRange(input, off, off + len));
 }
}

代码示例来源:origin: google/guava

@Override
protected HashCode makeHash() {
 return HashCode.fromBytes(out.toByteArray());
}

代码示例来源:origin: google/guava

@Override
protected HashCode makeHash() {
 return HashCode.fromBytes(out.toByteArray());
}

代码示例来源:origin: google/guava

@Override
public HashCode hashBytes(byte[] input, int off, int len) {
 return HashCode.fromBytes(Arrays.copyOfRange(input, off, off + len));
}

代码示例来源:origin: google/guava

/** Returns a {@link HashCode} for a sequence of longs, in big-endian order. */
private static HashCode toHashCode(long... longs) {
 ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN);
 for (long x : longs) {
  bb.putLong(x);
 }
 return HashCode.fromBytes(bb.array());
}

代码示例来源:origin: google/guava

private static void assertMacHashing(
  byte[] input, String algorithm, SecretKey key, HashFunction hashFunc) throws Exception {
 Mac mac = Mac.getInstance(algorithm);
 mac.init(key);
 mac.update(input);
 assertEquals(HashCode.fromBytes(mac.doFinal()), hashFunc.hashBytes(input));
 assertEquals(HashCode.fromBytes(mac.doFinal(input)), hashFunc.hashBytes(input));
}

代码示例来源:origin: google/guava

public void testFromBytes() {
 for (ExpectedHashCode expected : expectedHashCodes) {
  HashCode fromBytes = HashCode.fromBytes(expected.bytes);
  assertExpectedHashCode(expected, fromBytes);
 }
}

代码示例来源:origin: google/guava

public void testRoundTrip() {
 for (ExpectedHashCode expected : expectedHashCodes) {
  String string = HashCode.fromBytes(expected.bytes).toString();
  assertEquals(expected.toString, string);
  assertEquals(
    expected.toString,
    HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(string)).toString());
 }
}

代码示例来源:origin: google/guava

public void testEmptyInputs() throws Exception {
 String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
 Mac mac = Mac.getInstance("HmacMD5");
 mac.init(MD5_KEY);
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
 assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString());
}

代码示例来源:origin: google/guava

public void testKnownInputs_mixedAlgorithms() throws Exception {
 String knownOutput = "9753980fe94daa8ecaa82216519393a9";
 String input = "The quick brown fox jumps over the lazy dog";
 Mac mac = Mac.getInstance("HmacMD5");
 mac.init(SHA1_KEY);
 mac.update(input.getBytes(UTF_8));
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
 assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).hashString(input, UTF_8).toString());
 assertEquals(
   knownOutput, Hashing.hmacMd5(SHA1_KEY).hashBytes(input.getBytes(UTF_8)).toString());
}

代码示例来源:origin: google/guava

public void testKnownInputs() throws Exception {
 String knownOutput = "9753980fe94daa8ecaa82216519393a9";
 String input = "The quick brown fox jumps over the lazy dog";
 Mac mac = Mac.getInstance("HmacMD5");
 mac.init(MD5_KEY);
 mac.update(input.getBytes(UTF_8));
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
 assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashString(input, UTF_8).toString());
 assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashBytes(input.getBytes(UTF_8)).toString());
}

代码示例来源:origin: google/guava

public void testEmptyInputs_mixedAlgorithms() throws Exception {
 String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
 Mac mac = Mac.getInstance("HmacMD5");
 mac.init(SHA1_KEY);
 assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
 assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString());
}

代码示例来源:origin: google/guava

public void testMultipleUpdates() throws Exception {
 Mac mac = Mac.getInstance("HmacSHA1");
 mac.init(SHA1_KEY);
 mac.update("hello".getBytes(UTF_8));
 mac.update("world".getBytes(UTF_8));
 assertEquals(
   HashCode.fromBytes(mac.doFinal()),
   Hashing.hmacSha1(SHA1_KEY)
     .newHasher()
     .putString("hello", UTF_8)
     .putString("world", UTF_8)
     .hash());
}

代码示例来源: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: google/guava

public void testToString() {
 byte[] data = new byte[] {127, -128, 5, -1, 14};
 assertEquals("7f8005ff0e", HashCode.fromBytes(data).toString());
 assertEquals("7f8005ff0e", base16().lowerCase().encode(data));
}

代码示例来源:origin: google/guava

public void testMultipleUpdatesDoFinal() throws Exception {
 Mac mac = Mac.getInstance("HmacSHA1");
 mac.init(SHA1_KEY);
 mac.update("hello".getBytes(UTF_8));
 mac.update("world".getBytes(UTF_8));
 assertEquals(
   HashCode.fromBytes(mac.doFinal("!!!".getBytes(UTF_8))),
   Hashing.hmacSha1(SHA1_KEY)
     .newHasher()
     .putString("hello", UTF_8)
     .putString("world", UTF_8)
     .putString("!!!", UTF_8)
     .hash());
}

代码示例来源:origin: google/guava

public void testConcatenatingHashFunction_makeHash() {
 byte[] md5Hash = Hashing.md5().hashLong(42L).asBytes();
 byte[] murmur3Hash = Hashing.murmur3_32().hashLong(42L).asBytes();
 byte[] combined = new byte[md5Hash.length + murmur3Hash.length];
 ByteBuffer buffer = ByteBuffer.wrap(combined);
 buffer.put(md5Hash);
 buffer.put(murmur3Hash);
 HashCode expected = HashCode.fromBytes(combined);
 assertEquals(
   expected, Hashing.concatenating(Hashing.md5(), Hashing.murmur3_32()).hashLong(42L));
 assertEquals(
   expected, Hashing.concatenating(asList(Hashing.md5(), Hashing.murmur3_32())).hashLong(42L));
}

代码示例来源:origin: google/guava

public void testFromBytes_copyOccurs() {
 byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00};
 HashCode hashCode = HashCode.fromBytes(bytes);
 int expectedInt = 0x0000abcd;
 String expectedToString = "cdab0000";
 assertEquals(expectedInt, hashCode.asInt());
 assertEquals(expectedToString, hashCode.toString());
 bytes[0] = (byte) 0x00;
 assertEquals(expectedInt, hashCode.asInt());
 assertEquals(expectedToString, hashCode.toString());
}

代码示例来源:origin: google/guava

public void testCombineOrdered() {
 HashCode hash31 = HashCode.fromInt(31);
 HashCode hash32 = HashCode.fromInt(32);
 assertEquals(hash32, Hashing.combineOrdered(ImmutableList.of(hash32)));
 assertEquals(
   HashCode.fromBytes(new byte[] {(byte) 0x80, 0, 0, 0}),
   Hashing.combineOrdered(ImmutableList.of(hash32, hash32)));
 assertEquals(
   HashCode.fromBytes(new byte[] {(byte) 0xa0, 0, 0, 0}),
   Hashing.combineOrdered(ImmutableList.of(hash32, hash32, hash32)));
 assertFalse(
   Hashing.combineOrdered(ImmutableList.of(hash31, hash32))
     .equals(Hashing.combineOrdered(ImmutableList.of(hash32, hash31))));
}

代码示例来源: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());
}

相关文章