Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
private static boolean keysEqual(Key key1, Key key2) {
  if (key1.equals(key2)) {
    return true;
  }

  if (Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
    return true;
  }

  // More code for different types of keys here.
  // For example, the following code can check if
  // an RSAPrivateKey and an RSAPrivateCrtKey are equal:
  if ((key1 instanceof RSAPrivateKey) &&
      (key2 instanceof RSAPrivateKey)) {
  
    if ((((RSAKey) key1).getModulus().equals(((RSAKey) key2).getModulus())) &&
       (((RSAPrivateKey) key1).getPrivateExponent().equals(
         ((RSAPrivateKey) key2).getPrivateExponent()))) {
      return true;
    }
  }
  return false;
}

Exceptions

Wiki Markup
*MET12MET08-EX0:* This rule may be violated provided that the incompatible types are never compared.  There are classes in the Java platform libraries (and elsewhere) that extend an instantiable class by adding a value component.  For example, {{java.sql.Timestamp}} extends {{java.util.Date}} and adds a nanoseconds field. The {{equals}} implementation for {{Timestamp}} violates symmetry and can cause erratic behavior if {{Timestamp}} and {{Date}} objects are used in the same collection or are otherwise intermixed \[[Bloch 2008|AA. Bibliography#Bloch 08]\].

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MET12 MET08-J

low

unlikely

medium

P2

L3

Related Vulnerabilities

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8ed6b14710aef1ce-8133ad59-46574812-996fa2f3-ab31c217f39c0e11bf2bde79"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[method equals()

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dea51427e29a8458-955ee6e8-49754a54-88139198-5dce9e821117bc455b520793"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 8: Obey the general contract when overriding equals

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="202698d6bbd35aa0-2c3a03dd-423742c6-8649a0bc-cfa34de3c530661193cdacc6"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin 04]]

9.2 Overriding the equals method

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="56b2b78d1e3b9a55-c254a820-4b074407-97629517-46867abe74b7d16057943139"><ac:plain-text-body><![CDATA[

[[Harold 1997

AA. Bibliography#Harold 97]]

Chapter 3: Classes, Strings, and Arrays, The Object Class (equality)

]]></ac:plain-text-body></ac:structured-macro>

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0b8db39ce43d34f9-cbba451e-4b704ab2-ad2c97f0-d95d70f9ba3e3b3f7f1579cf"><ac:plain-text-body><![CDATA[

[[Techtalk 2007

AA. Bibliography#Techtalk 07]]

"More Joy of Sets"

]]></ac:plain-text-body></ac:structured-macro>

...