mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 14:24:46 +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) 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
|
||||
|
@ -29,6 +29,7 @@ import java.lang.ref.Reference;
|
|||
import java.security.MessageDigest;
|
||||
import java.security.KeyRep;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Arrays;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.DESKeySpec;
|
||||
|
||||
|
@ -108,26 +109,24 @@ final class DESKey implements 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;
|
||||
}
|
||||
return(retval ^= "des".hashCode());
|
||||
return Arrays.hashCode(this.key) ^ "des".hashCode();
|
||||
}
|
||||
|
||||
@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("DES")))
|
||||
return false;
|
||||
|
||||
byte[] thatKey = ((SecretKey)obj).getEncoded();
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
|
|
|
@ -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
|
||||
|
@ -29,6 +29,7 @@ import java.lang.ref.Reference;
|
|||
import java.security.MessageDigest;
|
||||
import java.security.KeyRep;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Arrays;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.DESedeKeySpec;
|
||||
|
||||
|
@ -107,27 +108,25 @@ final class DESedeKey implements 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;
|
||||
}
|
||||
return(retval ^= "desede".hashCode());
|
||||
return Arrays.hashCode(this.key) ^ "desede".hashCode();
|
||||
}
|
||||
|
||||
@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("DESede"))
|
||||
&& !(thatAlg.equalsIgnoreCase("TripleDES")))
|
||||
return false;
|
||||
|
||||
byte[] thatKey = ((SecretKey)obj).getEncoded();
|
||||
byte[] thatKey = that.getEncoded();
|
||||
boolean ret = MessageDigest.isEqual(this.key, thatKey);
|
||||
java.util.Arrays.fill(thatKey, (byte)0x00);
|
||||
return ret;
|
||||
|
|
|
@ -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
|
||||
|
@ -287,18 +287,18 @@ final class DHPrivateKey implements PrivateKey,
|
|||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, p, g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
|
||||
if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) {
|
||||
if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey other)) {
|
||||
return false;
|
||||
}
|
||||
javax.crypto.interfaces.DHPrivateKey other =
|
||||
(javax.crypto.interfaces.DHPrivateKey) obj;
|
||||
DHParameterSpec otherParams = other.getParams();
|
||||
return ((this.x.compareTo(other.getX()) == 0) &&
|
||||
(this.p.compareTo(otherParams.getP()) == 0) &&
|
||||
|
|
|
@ -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
|
||||
|
@ -279,19 +279,19 @@ javax.crypto.interfaces.DHPublicKey, Serializable {
|
|||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(y, p, g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
|
||||
if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
|
||||
if (!(obj instanceof javax.crypto.interfaces.DHPublicKey other)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
javax.crypto.interfaces.DHPublicKey other =
|
||||
(javax.crypto.interfaces.DHPublicKey) obj;
|
||||
DHParameterSpec otherParams = other.getParams();
|
||||
return ((this.y.compareTo(other.getY()) == 0) &&
|
||||
(this.p.compareTo(otherParams.getP()) == 0) &&
|
||||
|
|
|
@ -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
|
||||
|
@ -105,23 +105,20 @@ final class PBEKey implements 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;
|
||||
}
|
||||
return(retval ^= getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode());
|
||||
return Arrays.hashCode(this.key)
|
||||
^ getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
|
||||
if (!(obj instanceof SecretKey))
|
||||
if (!(obj instanceof SecretKey that))
|
||||
return false;
|
||||
|
||||
SecretKey that = (SecretKey)obj;
|
||||
|
||||
if (!(that.getAlgorithm().equalsIgnoreCase(type)))
|
||||
return false;
|
||||
|
||||
|
|
|
@ -165,7 +165,7 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
|
|||
@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()) &&
|
||||
|
@ -247,32 +247,28 @@ final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
|
|||
* Calculates a hash code value for the object.
|
||||
* Objects that are equal will also have the same hashcode.
|
||||
*/
|
||||
@Override
|
||||
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) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof SecretKey)) {
|
||||
if (!(obj instanceof SecretKey that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SecretKey that = (SecretKey) obj;
|
||||
|
||||
if (!(that.getAlgorithm().equalsIgnoreCase(getAlgorithm()))) {
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue