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