MCPcopy Create free account
hub / github.com/apache/tomcat / equals

Method equals

java/org/apache/tomcat/util/security/ConstantTime.java:47–78  ·  view source on GitHub ↗

Implements String equality which always compares all characters in the string, without stopping early if any characters do not match. Note: This implementation was adapted from MessageDigest#isEqual which we assume is as optimizer-defeating as possible. @param s1 The firs

(final String s1, final String s2, final boolean ignoreCase)

Source from the content-addressed store, hash-verified

45 * @return <code>true</code> if the strings are equal to each other, <code>false</code> otherwise.
46 */
47 public static boolean equals(final String s1, final String s2, final boolean ignoreCase) {
48 if (s1 == s2) {
49 return true;
50 }
51 if (s1 == null || s2 == null) {
52 return false;
53 }
54
55 final int len1 = s1.length();
56 final int len2 = s2.length();
57
58 if (len2 == 0) {
59 return len1 == 0;
60 }
61
62 int result = 0;
63 result |= len1 - len2;
64
65 // time-constant comparison
66 for (int i = 0; i < len1; i++) {
67 // If i >= len2, index2 is 0; otherwise, i.
68 final int index2 = ((i - len2) >>> 31) * i;
69 char c1 = s1.charAt(i);
70 char c2 = s2.charAt(index2);
71 if (ignoreCase) {
72 c1 = Character.toLowerCase(c1);
73 c2 = Character.toLowerCase(c2);
74 }
75 result |= c1 ^ c2;
76 }
77 return result == 0;
78 }
79
80
81 /**

Callers 6

prepareRequestMethod · 0.95
authenticateMethod · 0.95
matchesMethod · 0.95
equalsMethod · 0.95
validateMethod · 0.95

Calls 5

lengthMethod · 0.80
charAtMethod · 0.80
getLengthMethod · 0.65
getBytesMethod · 0.45
getStartMethod · 0.45

Tested by

no test coverage detected