mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8311170: Simplify and modernize equals and hashCode in security area
Reviewed-by: djelinski, rriggs, valeriep
This commit is contained in:
parent
e9f751ab16
commit
19ae62ae2c
96 changed files with 567 additions and 951 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
|
@ -76,16 +76,15 @@ final class CryptoAllPermission extends CryptoPermission {
|
|||
* @return {@code true} if <i>obj</i> is a
|
||||
* {@code CryptoAllPermission} object.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj == INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns the hash code value for this object.
|
||||
*
|
||||
* @return a hash code value for this object.
|
||||
* {@return the hash code value for this object}
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
|
@ -30,6 +30,7 @@ import java.security.*;
|
|||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.io.Serializable;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.crypto.spec.*;
|
||||
|
@ -252,6 +253,7 @@ class CryptoPermission extends java.security.Permission {
|
|||
* @param obj the object to test for equality with this object.
|
||||
* @return {@code true} if {@code obj} is equal to this object.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
@ -266,29 +268,20 @@ class CryptoPermission extends java.security.Permission {
|
|||
if (this.checkParam != that.checkParam) {
|
||||
return false;
|
||||
}
|
||||
return (equalObjects(this.exemptionMechanism,
|
||||
that.exemptionMechanism) &&
|
||||
equalObjects(this.algParamSpec,
|
||||
that.algParamSpec));
|
||||
return Objects.equals(this.exemptionMechanism, that.exemptionMechanism)
|
||||
&& Objects.equals(this.algParamSpec, that.algParamSpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hash code value for this object.
|
||||
*
|
||||
* @return a hash code value for this object.
|
||||
* {@return the hash code value for this object}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = alg.hashCode();
|
||||
retval ^= maxKeySize;
|
||||
if (exemptionMechanism != null) {
|
||||
retval ^= exemptionMechanism.hashCode();
|
||||
}
|
||||
if (checkParam) retval ^= 100;
|
||||
if (algParamSpec != null) {
|
||||
retval ^= algParamSpec.hashCode();
|
||||
}
|
||||
return retval;
|
||||
return alg.hashCode()
|
||||
^ maxKeySize
|
||||
^ Objects.hashCode(exemptionMechanism)
|
||||
^ (checkParam ? 100 : 0)
|
||||
^ Objects.hashCode(algParamSpec);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -437,14 +430,6 @@ class CryptoPermission extends java.security.Permission {
|
|||
return !this.checkParam;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean equalObjects(Object obj1, Object obj2) {
|
||||
if (obj1 == null) {
|
||||
return (obj2 == null);
|
||||
}
|
||||
|
||||
return obj1.equals(obj2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
|
@ -29,6 +29,7 @@ import java.io.*;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Objects;
|
||||
import java.util.Vector;
|
||||
import static java.util.Locale.ENGLISH;
|
||||
|
||||
|
@ -616,20 +617,17 @@ final class CryptoPolicyParser {
|
|||
* Calculates a hash code value for the object. Objects
|
||||
* which are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = cryptoPermission.hashCode();
|
||||
if (alg != null) retval ^= alg.hashCode();
|
||||
if (exemptionMechanism != null) {
|
||||
retval ^= exemptionMechanism.hashCode();
|
||||
}
|
||||
retval ^= maxKeySize;
|
||||
if (checkParam) retval ^= 100;
|
||||
if (algParamSpec != null) {
|
||||
retval ^= algParamSpec.hashCode();
|
||||
}
|
||||
return retval;
|
||||
return cryptoPermission.hashCode()
|
||||
^ Objects.hashCode(alg)
|
||||
^ Objects.hashCode(exemptionMechanism)
|
||||
^ maxKeySize
|
||||
^ (checkParam ? 100 : 0)
|
||||
^ Objects.hashCode(algParamSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
@ -637,12 +635,8 @@ final class CryptoPolicyParser {
|
|||
if (!(obj instanceof CryptoPermissionEntry that))
|
||||
return false;
|
||||
|
||||
if (this.cryptoPermission == null) {
|
||||
if (that.cryptoPermission != null) return false;
|
||||
} else {
|
||||
if (!this.cryptoPermission.equals(
|
||||
that.cryptoPermission))
|
||||
return false;
|
||||
if (!Objects.equals(this.cryptoPermission, that.cryptoPermission)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.alg == null) {
|
||||
|
@ -652,15 +646,11 @@ final class CryptoPolicyParser {
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!(this.maxKeySize == that.maxKeySize)) return false;
|
||||
if (this.maxKeySize != that.maxKeySize) return false;
|
||||
|
||||
if (this.checkParam != that.checkParam) return false;
|
||||
|
||||
if (this.algParamSpec == null) {
|
||||
return that.algParamSpec == null;
|
||||
} else {
|
||||
return this.algParamSpec.equals(that.algParamSpec);
|
||||
}
|
||||
return Objects.equals(this.algParamSpec, that.algParamSpec);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -26,6 +26,7 @@
|
|||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* This class specifies the parameters used with the
|
||||
|
@ -131,6 +132,7 @@ public class RC2ParameterSpec implements AlgorithmParameterSpec {
|
|||
* @return true if the objects are considered equal, false if
|
||||
* {@code obj} is null or otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
|
@ -140,20 +142,15 @@ public class RC2ParameterSpec implements AlgorithmParameterSpec {
|
|||
}
|
||||
|
||||
return ((effectiveKeyBits == other.effectiveKeyBits) &&
|
||||
java.util.Arrays.equals(iv, other.iv));
|
||||
Arrays.equals(iv, other.iv));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = 0;
|
||||
if (iv != null) {
|
||||
for (int i = 1; i < iv.length; i++) {
|
||||
retval += iv[i] * i;
|
||||
}
|
||||
}
|
||||
return retval + effectiveKeyBits;
|
||||
return Arrays.hashCode(iv) + effectiveKeyBits;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -26,6 +26,7 @@
|
|||
package javax.crypto.spec;
|
||||
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* This class specifies the parameters used with the
|
||||
|
@ -176,6 +177,7 @@ public class RC5ParameterSpec implements AlgorithmParameterSpec {
|
|||
* @return true if the objects are considered equal, false if
|
||||
* {@code obj} is null or otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
|
@ -187,21 +189,15 @@ public class RC5ParameterSpec implements AlgorithmParameterSpec {
|
|||
return ((version == other.version) &&
|
||||
(rounds == other.rounds) &&
|
||||
(wordSize == other.wordSize) &&
|
||||
java.util.Arrays.equals(iv, other.iv));
|
||||
Arrays.equals(iv, other.iv));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = 0;
|
||||
if (iv != null) {
|
||||
for (int i = 1; i < iv.length; i++) {
|
||||
retval += iv[i] * i;
|
||||
}
|
||||
}
|
||||
retval += (version + rounds + wordSize);
|
||||
return retval;
|
||||
return Arrays.hashCode(iv) + version + rounds + wordSize;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -198,11 +198,9 @@ public class SecretKeySpec implements KeySpec, SecretKey {
|
|||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = 0;
|
||||
for (int i = 1; i < this.key.length; i++) {
|
||||
retval += this.key[i] * i;
|
||||
}
|
||||
int retval = Arrays.hashCode(key);
|
||||
if (this.algorithm.equalsIgnoreCase("TripleDES"))
|
||||
return retval ^ "desede".hashCode();
|
||||
else
|
||||
|
@ -220,14 +218,15 @@ public class SecretKeySpec implements KeySpec, SecretKey {
|
|||
* @return true if the objects are considered equal, false if
|
||||
* <code>obj</code> is null or otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (!(obj instanceof SecretKey))
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
|
||||
String thatAlg = ((SecretKey)obj).getAlgorithm();
|
||||
String thatAlg = that.getAlgorithm();
|
||||
if (!(thatAlg.equalsIgnoreCase(this.algorithm))) {
|
||||
if ((!(thatAlg.equalsIgnoreCase("DESede"))
|
||||
|| !(this.algorithm.equalsIgnoreCase("TripleDES")))
|
||||
|
@ -236,7 +235,7 @@ public class SecretKeySpec implements KeySpec, SecretKey {
|
|||
return false;
|
||||
}
|
||||
|
||||
byte[] thatKey = ((SecretKey)obj).getEncoded();
|
||||
byte[] thatKey = that.getEncoded();
|
||||
try {
|
||||
return MessageDigest.isEqual(this.key, thatKey);
|
||||
} finally {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
|
@ -260,6 +260,7 @@ public final class PrivateCredentialPermission extends Permission {
|
|||
* has the same credential class as this object,
|
||||
* and has the same Principals as this object.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
@ -271,10 +272,9 @@ public final class PrivateCredentialPermission extends Permission {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the hash code value for this object.
|
||||
*
|
||||
* @return a hash code value for this object.
|
||||
* {@return the hash code value for this object}
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.credentialClass.hashCode();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -907,10 +907,6 @@ public final class Subject implements java.io.Serializable {
|
|||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
@ -1003,9 +999,7 @@ public final class Subject implements java.io.Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a hashcode for this {@code Subject}.
|
||||
*
|
||||
* @return a hashcode for this {@code Subject}.
|
||||
* {@return a hashcode for this {@code Subject}}
|
||||
*
|
||||
* @throws SecurityException if a security manager is installed and the
|
||||
* caller does not have a {@link PrivateCredentialPermission}
|
||||
|
@ -1486,6 +1480,7 @@ public final class Subject implements java.io.Serializable {
|
|||
return elements.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
|
@ -1507,14 +1502,11 @@ public final class Subject implements java.io.Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int h = 0;
|
||||
Iterator<E> i = iterator();
|
||||
while (i.hasNext()) {
|
||||
E obj = i.next();
|
||||
if (obj != null) {
|
||||
h += obj.hashCode();
|
||||
}
|
||||
for (E obj : this) {
|
||||
h += Objects.hashCode(obj);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
|
|
@ -457,6 +457,7 @@ public final class X500Principal implements Principal, java.io.Serializable {
|
|||
* @return {@code true} if the specified {@code Object} is equal
|
||||
* to this {@code X500Principal}, {@code false} otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
|
@ -468,13 +469,12 @@ public final class X500Principal implements Principal, java.io.Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return a hash code for this {@code X500Principal}.
|
||||
* {@return a hash code for this {@code X500Principal}}
|
||||
*
|
||||
* <p> The hash code is calculated via:
|
||||
* {@code getName(X500Principal.CANONICAL).hashCode()}
|
||||
*
|
||||
* @return a hash code for this {@code X500Principal}
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return thisX500Name.hashCode();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2020, 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
|
||||
|
@ -31,6 +31,7 @@ import java.security.NoSuchAlgorithmException;
|
|||
import java.security.NoSuchProviderException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.SignatureException;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* <p>Abstract class for managing a variety of identity certificates.
|
||||
|
@ -72,51 +73,38 @@ public abstract class Certificate {
|
|||
|
||||
/**
|
||||
* Compares this certificate for equality with the specified
|
||||
* object. If the {@code other} object is an
|
||||
* object. If the {@code obj} object is an
|
||||
* {@code instanceof} {@code Certificate}, then
|
||||
* its encoded form is retrieved and compared with the
|
||||
* encoded form of this certificate.
|
||||
*
|
||||
* @param other the object to test for equality with this certificate.
|
||||
* @param obj the object to test for equality with this certificate.
|
||||
* @return true if the encoded forms of the two certificates
|
||||
* match, false otherwise.
|
||||
*/
|
||||
public boolean equals(Object other) {
|
||||
if (this == other)
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!(other instanceof Certificate))
|
||||
if (!(obj instanceof Certificate other))
|
||||
return false;
|
||||
try {
|
||||
byte[] thisCert = this.getEncoded();
|
||||
byte[] otherCert = ((Certificate)other).getEncoded();
|
||||
|
||||
if (thisCert.length != otherCert.length)
|
||||
return false;
|
||||
for (int i = 0; i < thisCert.length; i++)
|
||||
if (thisCert[i] != otherCert[i])
|
||||
return false;
|
||||
return true;
|
||||
return Arrays.equals(this.getEncoded(), other.getEncoded());
|
||||
} catch (CertificateException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hashcode value for this certificate from its
|
||||
* encoded form.
|
||||
*
|
||||
* @return the hashcode value.
|
||||
* {@return a hashcode value for this certificate from
|
||||
* its encoded form}
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int retval = 0;
|
||||
try {
|
||||
byte[] certData = this.getEncoded();
|
||||
for (int i = 1; i < certData.length; i++) {
|
||||
retval += certData[i] * i;
|
||||
}
|
||||
return (retval);
|
||||
return Arrays.hashCode(this.getEncoded());
|
||||
} catch (CertificateException e) {
|
||||
return (retval);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue