Class ConstantTimeUtil

java.lang.Object
cloud.opencode.base.crypto.util.ConstantTimeUtil

public final class ConstantTimeUtil extends Object
Constant-time comparison utilities to prevent timing attacks - All operations execute in constant time regardless of input values 常量时间比较工具,防止时序攻击 - 所有操作都以恒定时间执行,与输入值无关

Features | 主要功能:

  • Constant-time comparison to prevent timing attacks - 常量时间比较以防止时序攻击

Usage Examples | 使用示例:

boolean equal = ConstantTimeUtil.equals(a, b);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(n) - 时间复杂度: O(n),n为数据长度,始终执行完整比较
  • Space complexity: O(1) - 空间复杂度: O(1)
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compare(byte[] a, byte[] b)
    Constant-time byte array comparison 常量时间字节数组比较
    static boolean
    equals(byte[] a, byte[] b)
    Constant-time byte array equality comparison 常量时间字节数组相等性比较
    static boolean
    Constant-time string equality comparison 常量时间字符串相等性比较

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • equals

      public static boolean equals(byte[] a, byte[] b)
      Constant-time byte array equality comparison 常量时间字节数组相等性比较

      This method executes in constant time to prevent timing attacks. It compares every byte regardless of whether a difference is found early. 此方法以恒定时间执行以防止时序攻击。无论是否提前发现差异,它都会比较每个字节。

      Parameters:
      a - first byte array
      b - second byte array
      Returns:
      true if arrays are equal in length and content, false otherwise
    • equals

      public static boolean equals(String a, String b)
      Constant-time string equality comparison 常量时间字符串相等性比较

      This method executes in constant time to prevent timing attacks. Strings are converted to UTF-8 bytes and compared byte by byte. 此方法以恒定时间执行以防止时序攻击。字符串转换为UTF-8字节并逐字节比较。

      Parameters:
      a - first string
      b - second string
      Returns:
      true if strings are equal, false otherwise
    • compare

      public static int compare(byte[] a, byte[] b)
      Constant-time byte array comparison 常量时间字节数组比较

      This method executes in constant time to prevent timing attacks. Returns a comparison result similar to Arrays.compare(boolean[], boolean[]). 此方法以恒定时间执行以防止时序攻击。返回类似于Arrays.compare(boolean[], boolean[])的比较结果。

      Parameters:
      a - first byte array
      b - second byte array
      Returns:
      negative if a < b, zero if a == b, positive if a > b
      Throws:
      NullPointerException - if either array is null