Skip to content

Commit

Permalink
TKSS-335: Backport JDK-8311170: Simplify and modernize equals and has…
Browse files Browse the repository at this point in the history
…hCode in security area
  • Loading branch information
johnshajiang committed Aug 14, 2023
1 parent 7d3e88a commit ac68654
Show file tree
Hide file tree
Showing 35 changed files with 227 additions and 307 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -106,11 +106,8 @@ public String getFormat() {
* Objects that are equal will also have the same hashcode.
*/
public int hashCode() {
int retval = 0;
for (int i = 1; i < this.key.length; i++) {
retval += this.key[i] * i;
}
return(retval ^= getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode());
return Arrays.hashCode(this.key)
^ getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
}

public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (this.getClass() != obj.getClass()) return false;
if (obj == null || this.getClass() != obj.getClass()) return false;
SecretKey sk = (SecretKey)obj;
return prf.getAlgorithm().equalsIgnoreCase(
sk.getAlgorithm()) &&
Expand Down Expand Up @@ -251,18 +251,15 @@ public String getFormat() {
*/
public int hashCode() {
try {
int retval = 0;
for (int i = 1; i < this.key.length; i++) {
retval += this.key[i] * i;
}
return (retval ^= getAlgorithm().toLowerCase
(Locale.ENGLISH).hashCode());
return Arrays.hashCode(this.key)
^ getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
} finally {
// prevent this from being cleaned for the above block
// Reference.reachabilityFence(this);
}
}

@Override
public boolean equals(Object obj) {
try {
if (obj == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private void readObject(ObjectInputStream stream) throws IOException {
* @return {@code true} if this key has the same encoding as the
* object argument; {@code false} otherwise.
*/
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
Expand Down Expand Up @@ -290,6 +291,7 @@ public boolean equals(Object object) {
* Calculates a hash code value for this object. Objects
* which are equal will also have the same hashcode.
*/
@Override
public int hashCode() {
return Arrays.hashCode(getEncodedInternal());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,6 +28,7 @@
import com.tencent.kona.jdk.internal.util.Preconditions;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;

/**
* A packed array of booleans.
Expand Down Expand Up @@ -68,7 +69,7 @@ public BitArray(int length) throws IllegalArgumentException {
* Creates a BitArray of the specified size, initialized from the
* specified byte array. The most significant bit of {@code a[0]} gets
* index zero in the BitArray. The array must be large enough to specify
* a value for every bit of the BitArray. i.e. {@code 8*a.length <= length}.
* a value for every bit of the BitArray, i.e. {@code 8*a.length >= length}.
*/
public BitArray(int length, byte[] a) throws IllegalArgumentException {
this(length, a, 0);
Expand All @@ -79,7 +80,7 @@ public BitArray(int length, byte[] a) throws IllegalArgumentException {
* specified byte array starting at the specified offset. The most
* significant bit of {@code a[ofs]} gets index zero in the BitArray.
* The array must be large enough to specify a value for every bit of
* the BitArray, i.e. {@code 8*(a.length - ofs) <= length}.
* the BitArray, i.e. {@code 8*(a.length - ofs) >= length}.
*/
public BitArray(int length, byte[] a, int ofs)
throws IllegalArgumentException {
Expand Down Expand Up @@ -177,18 +178,16 @@ public byte[] toByteArray() {
return repn.clone();
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof BitArray)) return false;

BitArray ba = (BitArray) obj;

if (ba.length != length) return false;

for (int i = 0; i < repn.length; i += 1) {
if (repn[i] != ba.repn[i]) return false;
if (!(obj instanceof BitArray)) {
return false;
}
return true;

return length == ((BitArray) obj).length
&& Arrays.equals(repn, ((BitArray) obj).repn);
}

/**
Expand All @@ -204,17 +203,11 @@ public boolean[] toBooleanArray() {
}

/**
* Returns a hash code value for this bit array.
*
* @return a hash code value for this bit array.
* {@return a hash code value for this bit array}
*/
@Override
public int hashCode() {
int hashCode = 0;

for (int i = 0; i < repn.length; i++)
hashCode = 31*hashCode + repn[i];

return hashCode ^ length;
return Arrays.hashCode(repn) ^ length;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1315,9 +1315,7 @@ public DerValue withTag(byte newTag) {
}

/**
* Returns a hashcode for this DerValue.
*
* @return a hashcode for this DerValue.
* {@return a hashcode for this DerValue}
*/
@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,7 @@ public final boolean equals(ObjectIdentifier id) {
}

/**
* Returns a hashcode for this AlgorithmId.
*
* @return a hashcode for this AlgorithmId.
* {@return a hashcode for this AlgorithmId}
*/
@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;

import com.tencent.kona.crypto.CryptoInsts;
import com.tencent.kona.sun.security.util.BitArray;
Expand Down Expand Up @@ -399,6 +400,7 @@ private void readObject(ObjectInputStream stream) throws IOException {
}
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,7 @@
package com.tencent.kona.sun.security.pkcs;

import java.io.*;
import java.util.Arrays;

import com.tencent.kona.sun.security.util.DerOutputStream;
import com.tencent.kona.sun.security.x509.AlgorithmId;
Expand Down Expand Up @@ -135,33 +136,23 @@ public byte[] getEncoded() {
return this.encoded.clone();
}

public boolean equals(Object other) {
if (this == other)
public boolean equals(Object obj) {
if (this == obj) {
return true;
if (!(other instanceof EncryptedPrivateKeyInfo))
}
if (!(obj instanceof EncryptedPrivateKeyInfo)) {
return false;
byte[] thisEncrInfo = this.getEncoded();
byte[] otherEncrInfo
= ((EncryptedPrivateKeyInfo) other).getEncoded();
}

if (thisEncrInfo.length != otherEncrInfo.length)
return false;
for (int i = 0; i < thisEncrInfo.length; i++)
if (thisEncrInfo[i] != otherEncrInfo[i])
return false;
return true;
return Arrays.equals(this.getEncoded(),
((EncryptedPrivateKeyInfo) obj).getEncoded());
}

/**
* Returns a hashcode for this EncryptedPrivateKeyInfo.
*
* @return a hashcode for this EncryptedPrivateKeyInfo.
* {@return a hashcode for this EncryptedPrivateKeyInfo}
*/
@Override
public int hashCode() {
int retval = 0;

for (int i = 0; i < this.encryptedData.length; i++)
retval += this.encryptedData[i] * i;
return retval;
return Arrays.hashCode(encryptedData);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -32,6 +32,7 @@

import java.security.*;

import java.util.Arrays;
import java.util.Base64;

import com.tencent.kona.crypto.CryptoInsts;
Expand Down Expand Up @@ -326,41 +327,36 @@ public String toString() {

/**
* Compares this object for equality with the specified
* object. If the <code>other</code> object is an
* object. If the <code>obj</code> object is an
* <code>instanceof</code> <code>PKCS10</code>, then
* its encoded form is retrieved and compared with the
* encoded form of this certificate request.
*
* @param other the object to test for equality with this object.
* @param obj the object to test for equality with this object.
* @return true iff the encoded forms of the two certificate
* requests match, false otherwise.
*/
public boolean equals(Object other) {
if (this == other)
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(other instanceof PKCS10))
if (!(obj instanceof PKCS10))
return false;
if (encoded == null) // not signed yet
return false;
byte[] otherEncoded = ((PKCS10)other).getEncoded();
byte[] otherEncoded = ((PKCS10)obj).getEncoded();
if (otherEncoded == null)
return false;

return java.util.Arrays.equals(encoded, otherEncoded);
return Arrays.equals(encoded, otherEncoded);
}

/**
* Returns a hashcode value for this certificate request from its
* encoded form.
*
* @return the hashcode value.
* {@return the hashcode value for this certificate request from its
* encoded form}
*/
@Override
public int hashCode() {
int retval = 0;
if (encoded != null)
for (int i = 1; i < encoded.length; i++)
retval += encoded[i] * i;
return(retval);
return Arrays.hashCode(encoded);
}

private X500Name subject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -158,34 +158,31 @@ public boolean equals(Object other) {
return true;
if (!(other instanceof PKCS10Attributes))
return false;

Collection<PKCS10Attribute> othersAttribs =
((PKCS10Attributes)other).getAttributes();
PKCS10Attribute[] attrs =
othersAttribs.toArray(new PKCS10Attribute[0]);
int len = attrs.length;
if (len != map.size())
return false;
PKCS10Attribute thisAttr, otherAttr;
PKCS10Attribute thisAttr;
String key;
for (int i=0; i < len; i++) {
otherAttr = attrs[i];
for (PKCS10Attribute otherAttr : attrs) {
key = otherAttr.getAttributeId().toString();

thisAttr = map.get(key);
if (thisAttr == null)
return false;
if (! thisAttr.equals(otherAttr))
if (!thisAttr.equals(otherAttr))
return false;
}
return true;
}

/**
* Returns a hashcode value for this PKCS10Attributes.
*
* @return the hashcode value.
* {@return the hashcode value for this PKCS10Attributes}
*/
@Override
public int hashCode() {
return map.hashCode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -178,19 +178,13 @@ public void encode(DerOutputStream out) {
}

/**
* Returns a hashcode value for this CertId.
*
* @return the hashcode value.
* {@return a hashcode value for this CertId}
*/
@Override public int hashCode() {
if (myhash == -1) {
myhash = hashAlgId.hashCode();
for (int i = 0; i < issuerNameHash.length; i++) {
myhash += issuerNameHash[i] * i;
}
for (int i = 0; i < issuerKeyHash.length; i++) {
myhash += issuerKeyHash[i] * i;
}
myhash += Arrays.hashCode(issuerNameHash);
myhash += Arrays.hashCode(issuerKeyHash);
myhash += certSerialNumber.getNumber().hashCode();
}
return myhash;
Expand Down
Loading

0 comments on commit ac68654

Please sign in to comment.