Class TOTP

java.lang.Object
org.aoju.bus.crypto.digest.otp.HOTP
org.aoju.bus.crypto.digest.otp.TOTP

public class TOTP extends HOTP

time-based one-time passwords (TOTP) 基于时间戳算法的一次性密码生成器, 规范见:RFC 6238

时间同步,基于客户端的动态口令和动态口令验证服务器的时间比对,一般每30秒产生一个新口令, 要求客户端和服务器能够十分精确的保持正确的时钟,客户端和服务端基于时间计算的动态口令才能一致

参考:https://github.com/jchambers/java-otp

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • DEFAULT_TIME_STEP

      public static final Duration DEFAULT_TIME_STEP
      默认步进 (30秒)
  • Constructor Details

    • TOTP

      public TOTP(byte[] key)
      构造,使用默认HMAC算法(HmacSHA1)
      Parameters:
      key - 共享密码,RFC 4226要求最少128位
    • TOTP

      public TOTP(Duration timeStep, byte[] key)
      构造,使用默认HMAC算法(HmacSHA1)
      Parameters:
      timeStep - 日期步进,用于生成移动因子(moving factor)
      key - 共享密码,RFC 4226要求最少128位
    • TOTP

      public TOTP(Duration timeStep, int passwordLength, byte[] key)
      构造,使用默认HMAC算法(HmacSHA1)
      Parameters:
      timeStep - 日期步进,用于生成移动因子(moving factor)
      passwordLength - 密码长度,可以是6,7,8
      key - 共享密码,RFC 4226要求最少128位
    • TOTP

      public TOTP(Duration timeStep, int passwordLength, org.aoju.bus.core.lang.Algorithm algorithm, byte[] key)
      构造
      Parameters:
      timeStep - 日期步进,用于生成移动因子(moving factor)
      passwordLength - 密码长度,可以是6,7,8
      algorithm - HMAC算法枚举
      key - 共享密码,RFC 4226要求最少128位
  • Method Details

    • generateGoogleSecretKey

      public static String generateGoogleSecretKey(String account, int numBytes)
      生成谷歌认证器的字符串(扫码字符串) 基于时间的,计数器不适合
      Parameters:
      account - 账户名
      numBytes - 将生成的种子字节数量
      Returns:
      共享密钥
    • generate

      public int generate(Instant timestamp)
      使用给定的时间戳生成一次性密码
      Parameters:
      timestamp - 用于生成密码的时间戳
      Returns:
      一次性密码的int形式
    • validate

      public boolean validate(Instant timestamp, int offsetSize, int code)
      用于验证code是否正确
      Parameters:
      timestamp - 验证时间戳
      offsetSize - 误差范围
      code - code
      Returns:
      是否通过
    • getTimeStep

      public Duration getTimeStep()
      获取步进
      Returns:
      步进