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

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

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

HashCode.equals介绍

[英]Returns true if object is a HashCode instance with the identical byte representation to this hash code.

Security note: this method uses a constant-time (not short-circuiting) implementation to protect against timing attacks.
[中]若对象是哈希代码实例,且其字节表示形式和此哈希代码相同,则返回true。
安全注意:此方法使用一个恒定时间(而不是短路)实现来防止timing attacks

代码示例

代码示例来源:origin: apache/incubator-druid

@Override
public boolean equals(Object o)
{
 if (this == o) {
  return true;
 }
 if (o == null || getClass() != o.getClass()) {
  return false;
 }
 BloomDimFilter that = (BloomDimFilter) o;
 if (!dimension.equals(that.dimension)) {
  return false;
 }
 if (hash != null ? !hash.equals(that.hash) : that.hash != null) {
  return false;
 }
 return extractionFn != null ? extractionFn.equals(that.extractionFn) : that.extractionFn == null;
}

代码示例来源: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 testCombineOrdered_randomHashCodes() {
 Random random = new Random(7);
 List<HashCode> hashCodes = Lists.newArrayList();
 for (int i = 0; i < 10; i++) {
  hashCodes.add(HashCode.fromLong(random.nextLong()));
 }
 HashCode hashCode1 = Hashing.combineOrdered(hashCodes);
 Collections.shuffle(hashCodes, random);
 HashCode hashCode2 = Hashing.combineOrdered(hashCodes);
 assertFalse(hashCode1.equals(hashCode2));
}

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

public static Model deserialize(Slice slice)
{
  int version = slice.getInt(VERSION_OFFSET);
  checkArgument(version == CURRENT_FORMAT_VERSION, format("Unsupported version: %d", version));
  byte[] modelHashBytes = slice.getBytes(HASH_OFFSET, 32);
  HashCode expectedHash = HashCode.fromBytes(modelHashBytes);
  HashCode actualHash = Hashing.sha256().hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET));
  checkArgument(actualHash.equals(expectedHash), "model hash does not match data");
  int id = slice.getInt(ALGORITHM_OFFSET);
  Class<? extends Model> algorithm = MODEL_SERIALIZATION_IDS.inverse().get(id);
  requireNonNull(algorithm, format("Unsupported algorith %d", id));
  int hyperparameterLength = slice.getInt(HYPERPARAMETER_LENGTH_OFFSET);
  byte[] hyperparameterBytes = slice.getBytes(HYPERPARAMETERS_OFFSET, hyperparameterLength);
  int dataLengthOffset = HYPERPARAMETERS_OFFSET + hyperparameterLength;
  long dataLength = slice.getLong(dataLengthOffset);
  int dataOffset = dataLengthOffset + SIZE_OF_LONG;
  byte[] data = slice.getBytes(dataOffset, (int) dataLength);
  try {
    Method deserialize = algorithm.getMethod("deserialize", byte[].class);
    return (Model) deserialize.invoke(null, new Object[] {data});
  }
  catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源: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 testObjectHashCodeWithSameLowOrderBytes() {
 // These will have the same first 4 bytes (all 0).
 byte[] bytesA = new byte[5];
 byte[] bytesB = new byte[5];
 // Change only the last (5th) byte
 bytesA[4] = (byte) 0xbe;
 bytesB[4] = (byte) 0xef;
 HashCode hashCodeA = HashCode.fromBytes(bytesA);
 HashCode hashCodeB = HashCode.fromBytes(bytesB);
 // They aren't equal...
 assertFalse(hashCodeA.equals(hashCodeB));
 // But they still have the same Object#hashCode() value.
 // Technically not a violation of the equals/hashCode contract, but...?
 assertEquals(hashCodeA.hashCode(), hashCodeB.hashCode());
}

代码示例来源:origin: spotify/heroic

if (!hashCode.equals(o.hashCode)) {
  return false;

代码示例来源:origin: io.takari/directory-watcher

private List<Path> findModifiedFiles(Set<Path> filesOnDisk) {
 List<Path> modifiedFileList = new ArrayList<Path>();
 for (Path file : filesOnDisk) {
  HashCode storedHashCode = hashCodeMap.get(file);
  HashCode newHashCode = hash(file);
  if (storedHashCode != null && !storedHashCode.equals(newHashCode) && newHashCode != null) {
   modifiedFileList.add(file);
   hashCodeMap.put(file, newHashCode);
  }
 }
 return modifiedFileList;
}

代码示例来源:origin: HotelsDotCom/styx

private boolean contentHashChanged(Path monitoredFile) {
  HashCode newHashCode = fileContentMd5(monitoredFile);
  boolean changed = !hashCode.getAndSet(newHashCode).equals(newHashCode);
  LOGGER.debug("contentHashChanged probe. Changed={}", changed);
  return changed;
}

代码示例来源:origin: xjdr/xio

@Override
 protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
  HashCode sentHash = HashCode.fromInt(msg.readInt());
  HashCode hash =
    Hashing.murmur3_32().hashBytes(msg.array(), msg.readerIndex(), msg.readableBytes());

  out.add(msg);

  if (sentHash.equals(hash)) {
   System.out.println("Hashes matches");
  } else {
   System.out.println("Hashes no matches");
  }
 }
}

代码示例来源:origin: apache/jackrabbit-oak

/**
 * To {@code Blob} instances are considered equal iff they have the
 * same SHA-256 hash code  are equal.
 * @param other
 */
@Override
public boolean equals(Object other) {
  if (other == this) {
    return true;
  }
  if (other instanceof AbstractBlob) {
    AbstractBlob that = (AbstractBlob) other;
    // optimize the comparison if both this and the other blob
    // already have pre-computed SHA-256 hash codes
    synchronized (this) {
      if (hashCode != null) {
        synchronized (that) {
          if (that.hashCode != null) {
            return hashCode.equals(that.hashCode);
          }
        }
      }
    }
  }
  return other instanceof Blob && equal(this, (Blob) other);
}

代码示例来源:origin: gradle.plugin.com.banderous.getpack/plugin

public static void install(Project project) {
 File f = project.file(PUPPET_PATH);
 try (InputStream in = InstallPuppet.class.getResourceAsStream("/unityPuppet.dll")) {
  Log.L.info("Unity puppet installed {}", f.exists());
  if (f.exists()) {
   HashCode currentHash = Files.hash(f, Hashing.md5());
   HashCode newHash = new HashingInputStream(Hashing.md5(), in).hash();
   boolean upToDate = currentHash.equals(newHash);
   Log.L.info("Unity puppet up to date {}", upToDate);
   if (upToDate) {
    return;
   }
  }
  Files.createParentDirs(f);
  try (FileOutputStream o = new FileOutputStream(f)) {
   ByteStreams.copy(in, o);
  }
 } catch (IOException e) {
  throw new UncheckedIOException(e);
 }
}

代码示例来源:origin: prestosql/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: prestosql/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: prestosql/presto

public static Model deserialize(Slice slice)
{
  int version = slice.getInt(VERSION_OFFSET);
  checkArgument(version == CURRENT_FORMAT_VERSION, format("Unsupported version: %d", version));
  byte[] modelHashBytes = slice.getBytes(HASH_OFFSET, 32);
  HashCode expectedHash = HashCode.fromBytes(modelHashBytes);
  HashCode actualHash = Hashing.sha256().hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET));
  checkArgument(actualHash.equals(expectedHash), "model hash does not match data");
  int id = slice.getInt(ALGORITHM_OFFSET);
  Class<? extends Model> algorithm = MODEL_SERIALIZATION_IDS.inverse().get(id);
  requireNonNull(algorithm, format("Unsupported algorith %d", id));
  int hyperparameterLength = slice.getInt(HYPERPARAMETER_LENGTH_OFFSET);
  byte[] hyperparameterBytes = slice.getBytes(HYPERPARAMETERS_OFFSET, hyperparameterLength);
  int dataLengthOffset = HYPERPARAMETERS_OFFSET + hyperparameterLength;
  long dataLength = slice.getLong(dataLengthOffset);
  int dataOffset = dataLengthOffset + SIZE_OF_LONG;
  byte[] data = slice.getBytes(dataOffset, (int) dataLength);
  try {
    Method deserialize = algorithm.getMethod("deserialize", byte[].class);
    return (Model) deserialize.invoke(null, new Object[] {data});
  }
  catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
    throw new RuntimeException(e);
  }
}

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

public void testCombineOrdered_randomHashCodes() {
 Random random = new Random(7);
 List<HashCode> hashCodes = Lists.newArrayList();
 for (int i = 0; i < 10; i++) {
  hashCodes.add(HashCode.fromLong(random.nextLong()));
 }
 HashCode hashCode1 = Hashing.combineOrdered(hashCodes);
 Collections.shuffle(hashCodes, random);
 HashCode hashCode2 = Hashing.combineOrdered(hashCodes);
 assertFalse(hashCode1.equals(hashCode2));
}

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

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: com.google.guava/guava-tests

public void testObjectHashCodeWithSameLowOrderBytes() {
 // These will have the same first 4 bytes (all 0).
 byte[] bytesA = new byte[5];
 byte[] bytesB = new byte[5];
 // Change only the last (5th) byte
 bytesA[4] = (byte) 0xbe;
 bytesB[4] = (byte) 0xef;
 HashCode hashCodeA = HashCode.fromBytes(bytesA);
 HashCode hashCodeB = HashCode.fromBytes(bytesB);
 // They aren't equal...
 assertFalse(hashCodeA.equals(hashCodeB));
 // But they still have the same Object#hashCode() value.
 // Technically not a violation of the equals/hashCode contract, but...?
 assertEquals(hashCodeA.hashCode(), hashCodeB.hashCode());
}

代码示例来源:origin: boonproject/boon

@Test
public void testWithField() {
  Employee rick = new Employee();
  String actualJson = toJson(rick);
  puts(actualJson);
  /* Convert it back to an employee. */
  Map<String, Object> fromJson = (Map<String, Object>) fromJson(actualJson);
  /* Grab the password an convert it into a HashCode. */
  String password = idxStr(fromJson, "password");
  fromJson = Maps.copy(fromJson);
  /* Convert and shove it back into the map. */
  idx(fromJson, "password", HashCode.fromString(password));
  /* Now convert the map into an Employee. */
  Employee rick2 = fromMap(fromJson, Employee.class);
  ok = rick.firstName == rick2.firstName.intern() || die();
  ok = rick.password.equals(rick2.password) || die(rick2.password);
}

相关文章