Java Code Examples for com.google.common.hash.HashCode#bits()

The following examples show how to use com.google.common.hash.HashCode#bits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpCommonDescriptionArg.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a sha256 from a string
 *
 * @param sha256 A hex representation of a sha256 hash
 * @param target The target to use in error messages
 * @return The hash
 * @throws HumanReadableException If the string could not be parsed, or if it's of the wrong
 *     length
 */
static HashCode parseSha256(String sha256, BuildTarget target) throws HumanReadableException {
  HashCode sha256Code;
  try {
    sha256Code = HashCode.fromString(sha256);
  } catch (IllegalArgumentException e) {
    throw new HumanReadableException(
        e,
        "%s when parsing sha256 of %s",
        e.getMessage(),
        target.getUnflavoredBuildTarget().getFullyQualifiedName());
  }

  if (sha256Code.bits() != 256) {
    throw new HumanReadableException(
        "%s does not appear to be a sha256 hash. Expected 256 bits, got %s bits when parsing %s",
        sha256, sha256Code.bits(), target.getUnflavoredBuildTarget().getFullyQualifiedName());
  }
  return sha256Code;
}