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

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

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

HashCode.toString介绍

[英]Returns a string containing each byte of #asBytes, in order, as a two-digit unsigned hexadecimal number in lower case.

Note that if the output is considered to be a single hexadecimal number, this hash code's bytes are the big-endian representation of that number. This may be surprising since everything else in the hashing API uniformly treats multibyte values as little-endian. But this format conveniently matches that of utilities such as the UNIX md5sum command.

To create a HashCode from its string representation, see #fromString.
[中]按顺序返回包含#asBytes的每个字节的字符串,以两位无符号十六进制数的小写形式返回。
请注意,如果将输出视为单个十六进制数,则此哈希代码的字节是该数字的大端表示形式。这可能令人惊讶,因为哈希API中的所有其他内容都将多字节值统一视为小端值。但这种格式可以方便地与UNIX md5sum命令等实用程序的格式相匹配。
要从其字符串表示形式创建哈希代码,请参见#fromString。

代码示例

代码示例来源: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 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 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() 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 testHash() throws IOException {
 File asciiFile = getTestFile("ascii.txt");
 File i18nFile = getTestFile("i18n.txt");
 String init = "d41d8cd98f00b204e9800998ecf8427e";
 assertEquals(init, Hashing.md5().newHasher().hash().toString());
 String asciiHash = "e5df5a39f2b8cb71b24e1d8038f93131";
 assertEquals(asciiHash, Files.hash(asciiFile, Hashing.md5()).toString());
 String i18nHash = "7fa826962ce2079c8334cd4ebf33aea4";
 assertEquals(i18nHash, Files.hash(i18nFile, Hashing.md5()).toString());
}

代码示例来源: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 testHash() throws IOException {
 ByteSource byteSource = new TestByteSource("hamburger\n".getBytes(Charsets.US_ASCII));
 // Pasted this expected string from `echo hamburger | md5sum`
 assertEquals("cfa0c5002275c90508338a5cdb2a9781", byteSource.hash(Hashing.md5()).toString());
}

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

public void testFromStringFailsWithUpperCaseString() {
 String string = Hashing.sha1().hashString("foo", Charsets.US_ASCII).toString().toUpperCase();
 try {
  HashCode.fromString(string);
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

public void testHashTwice() {
 Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher();
 assertEquals(
   "9753980fe94daa8ecaa82216519393a9",
   hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
 try {
  hasher.hash();
  fail();
 } catch (IllegalStateException expected) {
 }
}

代码示例来源: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 testRoundTripHashCodeUsingFromString() {
 HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
 HashCode hash2 = HashCode.fromString(hash1.toString());
 assertEquals(hash1, hash2);
}

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

public void testHashTwice() {
 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.hash();
  fail();
 } catch (IllegalStateException expected) {
 }
}

代码示例来源: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 testPutAfterHash() {
 Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher();
 assertEquals(
   "9753980fe94daa8ecaa82216519393a9",
   hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
 try {
  hasher.putInt(42);
  fail();
 } catch (IllegalStateException expected) {
 }
}

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

public void testKnownUtf8Hashing() {
 for (Cell<HashFunction, String, String> cell : KNOWN_HASHES.cellSet()) {
  HashFunction func = cell.getRowKey();
  String input = cell.getColumnKey();
  String expected = cell.getValue();
  assertEquals(
    String.format(Locale.ROOT, "Known hash for hash(%s, UTF_8) failed", input),
    expected,
    func.hashString(input, UTF_8).toString());
 }
}

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

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

public void testFromBytesNoCopy_noCopyOccurs() {
 byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00};
 HashCode hashCode = HashCode.fromBytesNoCopy(bytes);
 assertEquals(0x0000abcd, hashCode.asInt());
 assertEquals("cdab0000", hashCode.toString());
 bytes[0] = (byte) 0x00;
 assertEquals(0x0000ab00, hashCode.asInt());
 assertEquals("00ab0000", hashCode.toString());
}

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

public void testKnownValues() {
 assertHash(0, 0x629942693e10f867L, 0x92db0b82baeb5347L, "hell");
 assertHash(1, 0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello");
 assertHash(2, 0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello ");
 assertHash(3, 0x2ea59f466f6bed8cL, 0xc610990acc428a17L, "hello w");
 assertHash(4, 0x79f6305a386c572cL, 0x46305aed3483b94eL, "hello wo");
 assertHash(5, 0xc2219d213ec1f1b5L, 0xa1d8e2e0a52785bdL, "hello wor");
 assertHash(
   0, 0xe34bbc7bbc071b6cL, 0x7a433ca9c49a9347L, "The quick brown fox jumps over the lazy dog");
 assertHash(
   0, 0x658ca970ff85269aL, 0x43fee3eaa68e5c3eL, "The quick brown fox jumps over the lazy cog");
 // Known output from Python smhasher
 HashCode foxHash =
   murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8);
 assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.toString());
}

相关文章