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

Pavel Rappo prappo at openjdk.org
Mon Jul 17 12:26:13 UTC 2023


On Mon, 17 Jul 2023 11:02:46 GMT, Daniel Jeliński <djelinski at openjdk.org> wrote:

>> You think it was a typo (+ instead of *) in the original code?
>
> 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;

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

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


More information about the security-dev mailing list