...
Wiki Markup |
---|
Additionally, the {{URI}} class also performs normalization (removing extraneous path segments like '..') and relativization of paths \[[API 2006|AA. Bibliography#API 06]\] and \[[Darwin 2004|AA. Bibliography#Darwin 04]\]. |
Noncompliant Code Example (java.security.Key
)
The method java.lang.Object.equals()
by default, is unable to compare composite objects such as cryptographic keys. Most Key
classes lack an equals()
implementation that overrides Object's default implementation. In such cases, the components of the composite object must be compared individually to ensure correctness.
This noncompliant code example compares two keys using the equals()
method. The comparison may return false even when the key instances represent the same logical key.
Code Block | ||
---|---|---|
| ||
private static boolean keysEqual(Key key1, Key key2) {
if (key1.equals(key2)) {
return true;
}
}
|
Compliant Solution (java.security.Key
)
Wiki Markup |
---|
This compliant solution uses the {{equals()}} method as a first test, then compares the encoded version of the keys to facilitate provider-independent behavior. For example, this code can determine whether a {{RSAPrivateKey}} and {{RSAPrivateCrtKey}} represent equivalent private keys \[[Sun 2006|AA. Bibliography#Sun 06]\]. |
Code Block | ||
---|---|---|
| ||
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 |
---|
*MET12-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]\]. |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="37f5059a006e0c41-dc5f2b5a-4250462b-a0c5b7b3-e0c2f8b41b965b4980a3cc8c"><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="fe062e753f11d7dd-30c07784-4a7543d4-8b33b25b-87d9a31277266f1b05f227fd"><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="27a215f7f7a5741b-2808edd7-4201466f-93ebb814-b2ce8c19057b501f45eedf4d"><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="b938f6bc-53e9-451e-8ddf-1fef0f841f68"><ac"cdf6a506-d66b-4e87-aab7-062543c5bcf5"><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><![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> |
Wiki Markup |
---|
\[[Sun 2006|AA. Bibliography#Sun 06]\] [Determining If Two Keys Are Equal|http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Determining%20If%20Two%20Keys%20Are%20Equal] (JCA Reference Guide) |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b99865c97e7d3452-276ad581-46484b8d-91c78c75-1218796d680715683764faef"><ac:plain-text-body><![CDATA[ | [[Techtalk 2007 | AA. Bibliography#Techtalk 07]] | "More Joy of Sets" | ]]></ac:plain-text-body></ac:structured-macro> |
...