RFR: 8311170: Simplify and modernize equals and hashCode in security area [v9]

Daniel Jeliński djelinski at openjdk.org
Mon Jul 17 18:43:17 UTC 2023


On Mon, 17 Jul 2023 18:13:38 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

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

I think `oid.hashCode() + Arrays.hashCode(nameValue)` would have been good enough, but deepHashCode is also acceptable.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/14738#discussion_r1265763292


More information about the security-dev mailing list