RFR: 8311170: Simplify and modernize equals and hashCode in security area [v9]
Pavel Rappo
prappo at openjdk.org
Mon Jul 17 18:17:15 UTC 2023
On Mon, 17 Jul 2023 12:23:17 GMT, Pavel Rappo <prappo at openjdk.org> wrote:
>> The original code calculated `(37 ** nameValue.length)*(37+oid.hashCode)+Arrays.hashCode`; since you already dropped the multiplication, you can also drop the addition.
>
> Thanks for noticing this difference!
>
> Yes, the initial value would've been exponentiated (^ or ** in your notation). However, I note that the original code computed this:
>
> if nameValue.length > 0
>
> (37^nameValue.length) * oid.hashCode() + 37 + Arrays.hashCode(nameValue)
>
> otherwise
>
> 37 + oid.hashCode()
>
> Well, it would've computed that, had the multiplier constant been 37 in Arrays.hashCode, which it is not; it's 31.
>
> Since we cannot achieve absolute fidelity with the old code, I suggest we do this instead, what do you think?
>
> public int hashCode() {
> if (myhash == -1) {
> myhash = Arrays.deepHashCode(new Object[]{oid, nameValue});
> }
> return myhash;
Correction. If the multiplier were 31, then it would've been this:
return ( 30 + oid.hashCode() ) * 31**nameValue.length + Arrays.hashCode(nameValue);
^ ^
@djelinski, do you think we should use this instead?
return Arrays.deepHashCode(new Object[]{oid, nameValue});
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/14738#discussion_r1265737576
More information about the security-dev
mailing list