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

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

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

HashCode.fromString介绍

[英]Creates a HashCode from a hexadecimal ( base 16) encoded string. The string must be at least 2 characters long, and contain only valid, lower-cased hexadecimal characters.

This method accepts the exact format generated by #toString. If you require more lenient base 16 decoding, please use com.google.common.io.BaseEncoding#decode(and pass the result to #fromBytes).
[中]从十六进制(以16为基数)编码的字符串创建哈希代码。字符串长度必须至少为2个字符,并且仅包含有效的小写十六进制字符。
此方法接受#toString生成的精确格式。如果您需要更宽松的base 16解码,请使用com。谷歌。常见的木卫一。BaseEncoding#解码(并将结果传递给#fromBytes)。

代码示例

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

public void testFromStringFailsWithShortInputs() {
 try {
  HashCode.fromString("");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  HashCode.fromString("7");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 HashCode unused = HashCode.fromString("7f");
}

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

public void testFromStringFailsWithOddLengthInput() {
 try {
  HashCode.fromString("7f8");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

public void testFromStringFailsWithInvalidHexChar() {
 try {
  HashCode.fromString("7f8005ff0z");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) {
 assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data));
}

代码示例来源:origin: prestodb/presto

@DELETE
@Path("/v1/proxy")
@Produces(APPLICATION_JSON)
public void cancelQuery(
    @QueryParam("uri") String uri,
    @QueryParam("hmac") String hash,
    @Context HttpServletRequest servletRequest,
    @Suspended AsyncResponse asyncResponse)
{
  if (!hmac.hashString(uri, UTF_8).equals(HashCode.fromString(hash))) {
    throw badRequest(FORBIDDEN, "Failed to validate HMAC of URI");
  }
  Request.Builder request = prepareDelete().setUri(URI.create(uri));
  performRequest(servletRequest, asyncResponse, request, response -> responseWithHeaders(noContent(), response));
}

代码示例来源:origin: prestodb/presto

@GET
@Path("/v1/proxy")
@Produces(APPLICATION_JSON)
public void getNext(
    @QueryParam("uri") String uri,
    @QueryParam("hmac") String hash,
    @Context HttpServletRequest servletRequest,
    @Context UriInfo uriInfo,
    @Suspended AsyncResponse asyncResponse)
{
  if (!hmac.hashString(uri, UTF_8).equals(HashCode.fromString(hash))) {
    throw badRequest(FORBIDDEN, "Failed to validate HMAC of URI");
  }
  Request.Builder request = prepareGet().setUri(URI.create(uri));
  performRequest(servletRequest, asyncResponse, request, response -> buildResponse(uriInfo, response));
}

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

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-guava

@Override
  protected HashCode _deserialize(String value, DeserializationContext ctxt)
      throws IOException {
    return HashCode.fromString(value.toLowerCase(Locale.ENGLISH));
  }
}

代码示例来源:origin: FasterXML/jackson-datatypes-collections

@Override
  protected HashCode _deserialize(String value, DeserializationContext ctxt)
      throws IOException {
    return HashCode.fromString(value.toLowerCase(Locale.ENGLISH));
  }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testFromStringFailsWithShortInputs() {
 try {
  HashCode.fromString("");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  HashCode.fromString("7");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 HashCode unused = HashCode.fromString("7f");
}

代码示例来源:origin: org.sonatype.nexus/nexus-repository

/**
 * Extract checksums of asset blob if checksums are present in asset attributes.
 */
public Map<HashAlgorithm, HashCode> getChecksums(final Iterable<HashAlgorithm> hashAlgorithms)
{
 final NestedAttributesMap checksumAttributes = attributes().child(CHECKSUM);
 final Map<HashAlgorithm, HashCode> hashCodes = Maps.newHashMap();
 for (HashAlgorithm algorithm : hashAlgorithms) {
  final HashCode hashCode = HashCode.fromString(checksumAttributes.require(algorithm.name(), String.class));
  hashCodes.put(algorithm, hashCode);
 }
 return hashCodes;
}

代码示例来源:origin: com.google.guava/guava-tests

public void testFromStringFailsWithInvalidHexChar() {
 try {
  HashCode.fromString("7f8005ff0z");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testFromStringFailsWithOddLengthInput() {
 try {
  HashCode.fromString("7f8");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: org.sonatype.nexus/nexus-repository

/**
 * Extract checksum of asset blob if checksum is present in asset attributes.
 */
@Nullable
public HashCode getChecksum(final HashAlgorithm hashAlgorithm)
{
 String hashCode = attributes().child(CHECKSUM).get(hashAlgorithm.name(), String.class);
 if (hashCode != null) {
  return HashCode.fromString(hashCode);
 }
 return null;
}

代码示例来源:origin: com.google.guava/guava-tests

private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) {
 assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data));
}

代码示例来源:origin: com.google.guava/guava-tests

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

代码示例来源:origin: com.google.guava/guava-tests

public void testRoundTripHashCodeUsingFromString() {
 HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
 HashCode hash2 = HashCode.fromString(hash1.toString());
 assertEquals(hash1, hash2);
}

代码示例来源:origin: dhis2/dhis2-core

private Blob createBlob( FileResource fileResource, File file )
{
  return blobStore.blobBuilder( fileResource.getStorageKey() )
    .payload( file )
    .contentLength( fileResource.getContentLength() )
    .contentMD5( HashCode.fromString( fileResource.getContentMd5() ) )
    .contentType( fileResource.getContentType() )
    .contentDisposition( "filename=" + fileResource.getName() )
    .build();
}

代码示例来源:origin: dhis2/dhis2-core

private Blob createBlob( FileResource fileResource, byte[] bytes )
{
  return blobStore.blobBuilder( fileResource.getStorageKey() )
    .payload( bytes )
    .contentLength( fileResource.getContentLength() )
    .contentMD5( HashCode.fromString( fileResource.getContentMd5() ) )
    .contentType( fileResource.getContentType() )
    .contentDisposition( "filename=" + fileResource.getName() )
    .build();
}

相关文章