8311170: Simplify and modernize equals and hashCode in security area

Reviewed-by: djelinski, rriggs, valeriep
This commit is contained in:
Pavel Rappo 2023-08-09 12:34:40 +00:00
parent e9f751ab16
commit 19ae62ae2c
96 changed files with 567 additions and 951 deletions

View file

@ -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;
}

View file

@ -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);
}
}
/**

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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 {