Class ConstantTime
java.lang.Object
org.apache.tomcat.util.security.ConstantTime
Utility class for methods that, for security reasons, need to run in - as far as practical - constant time.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequals(byte[] b1, byte[] b2) Implements byte-array equality which always compares all bytes in the array, without stopping early if any bytes do not match.static booleanImplements String equality which always compares all characters in the string, without stopping early if any characters do not match.static booleanImplements ByteChunk / String equality which always compares all characters, without stopping early if any characters do not match.
-
Method Details
-
equals
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(byte[], byte[])which we assume is as optimizer-defeating as possible.- Parameters:
s1- The first string to compare.s2- The second string to compare.ignoreCase-trueif the strings should be compared without regard to case. Note that "true" here is only guaranteed to work with plain ASCII characters.- Returns:
trueif the strings are equal to each other,falseotherwise.
-
equals
Implements ByteChunk / String equality which always compares all characters, without stopping early if any characters do not match.Note: This implementation was adapted from
MessageDigest.isEqual(byte[], byte[])which we assume is as optimizer-defeating as possible.- Parameters:
bc- The ByteChunk to compare.s- The string to compare.- Returns:
trueif the strings are equal to each other,falseotherwise.
-
equals
public static boolean equals(byte[] b1, byte[] b2) Implements byte-array equality which always compares all bytes in the array, without stopping early if any bytes do not match.Note: Implementation note: this method delegates to
MessageDigest.isEqual(byte[], byte[])under the assumption that it provides a constant-time comparison of the bytes in the arrays. Java 7+ has such an implementation, but neither the Javadoc nor any specification requires it. Therefore, Tomcat should continue to use this method internally in case the JDK implementation changes so this method can be re-implemented properly.- Parameters:
b1- The first array to compare.b2- The second array to compare.- Returns:
trueif the arrays are equal to each other,falseotherwise.
-