Package io.netty.util.internal
Class ConstantTimeUtils
- java.lang.Object
-
- io.netty.util.internal.ConstantTimeUtils
-
public final class ConstantTimeUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intequalsConstantTime(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality without leaking timing information.static intequalsConstantTime(int x, int y)Compare twoints without leaking timing information.static intequalsConstantTime(long x, long y)Compare twolongss without leaking timing information.static intequalsConstantTime(java.lang.CharSequence s1, java.lang.CharSequence s2)Compare twoCharSequenceobjects without leaking timing information.
-
-
-
Method Detail
-
equalsConstantTime
public static int equalsConstantTime(int x, int y)Compare twoints without leaking timing information.The
intreturn type is intentional and is designed to allow cascading of constant time operations:int l1 = 1; int l2 = 1; int l3 = 1; int l4 = 500; boolean equals = (equalsConstantTime(l1, l2) & equalsConstantTime(l3, l4)) != 0;- Parameters:
x- the first value.y- the second value.- Returns:
0if not equal.1if equal.
-
equalsConstantTime
public static int equalsConstantTime(long x, long y)Compare twolongss without leaking timing information.The
intreturn type is intentional and is designed to allow cascading of constant time operations:long l1 = 1; long l2 = 1; long l3 = 1; long l4 = 500; boolean equals = (equalsConstantTime(l1, l2) & equalsConstantTime(l3, l4)) != 0;- Parameters:
x- the first value.y- the second value.- Returns:
0if not equal.1if equal.
-
equalsConstantTime
public static int equalsConstantTime(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality without leaking timing information. For performance reasons no bounds checking on the parameters is performed.The
intreturn type is intentional and is designed to allow cascading of constant time operations:byte[] s1 = new {1, 2, 3}; byte[] s2 = new {1, 2, 3}; byte[] s3 = new {1, 2, 3}; byte[] s4 = new {4, 5, 6}; boolean equals = (equalsConstantTime(s1, 0, s2, 0, s1.length) & equalsConstantTime(s3, 0, s4, 0, s3.length)) != 0;- Parameters:
bytes1- the first byte array.startPos1- the position (inclusive) to start comparing inbytes1.bytes2- the second byte array.startPos2- the position (inclusive) to start comparing inbytes2.length- the amount of bytes to compare. This is assumed to be validated as not going out of bounds by the caller.- Returns:
0if not equal.1if equal.
-
equalsConstantTime
public static int equalsConstantTime(java.lang.CharSequence s1, java.lang.CharSequence s2)Compare twoCharSequenceobjects without leaking timing information.The
intreturn type is intentional and is designed to allow cascading of constant time operations:String s1 = "foo"; String s2 = "foo"; String s3 = "foo"; String s4 = "goo"; boolean equals = (equalsConstantTime(s1, s2) & equalsConstantTime(s3, s4)) != 0;- Parameters:
s1- the first value.s2- the second value.- Returns:
0if not equal.1if equal.
-
-