public static boolean safeEquals ( String a , String b ) { if ( a . length ( ) != b . length ( ) ) { return false ; } else { char equal = 0 ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { equal |= a . charAt ( i ) ^ b . charAt ( i ) ; } return equal == 0 ; } }
Constant time for same length String comparison to prevent timing attacks .