类 SaTotpTemplate

java.lang.Object
cn.dev33.satoken.secure.totp.SaTotpTemplate

public class SaTotpTemplate extends Object
TOTP 算法类,支持 生成/验证 动态一次性密码
从以下版本开始:
1.42.0
作者:
click33
  • 字段详细资料

    • timeStep

      public int timeStep
      时间窗口步长(秒)
    • codeDigits

      public int codeDigits
      生成的验证码位数
    • hmacAlgorithm

      public String hmacAlgorithm
      哈希算法(HmacSHA1、HmacSHA256等)
    • secretKeyLength

      public int secretKeyLength
      密钥长度(字节,推荐16或32)
  • 构造器详细资料

    • SaTotpTemplate

      public SaTotpTemplate()
      构造函数 (使用默认参数)
    • SaTotpTemplate

      public SaTotpTemplate(int timeStep, int codeDigits, String hmacAlgorithm, int secretKeyLength)
      构造函数 (使用自定义参数)
      参数:
      timeStep - 时间窗口步长(秒)
      codeDigits - 生成的验证码位数
      hmacAlgorithm - 哈希算法(HmacSHA1、HmacSHA256等)
      secretKeyLength - 密钥长度(字节,推荐16或32)
  • 方法详细资料

    • generateSecretKey

      public String generateSecretKey()
      生成随机密钥(Base32编码)
      返回:
      /
    • _generateTOTP

      public String _generateTOTP(String secretKey)
      生成当前时间的 TOTP 验证码
      参数:
      secretKey - Base32 编码的密钥
      返回:
      /
    • validateTOTP

      public boolean validateTOTP(String secretKey, String code, int timeWindowOffset)
      判断用户输入的 TOTP 是否有效
      参数:
      secretKey - Base32编码的密钥
      code - 用户输入的验证码
      timeWindowOffset - 允许的时间窗口偏移量(如1表示允许前后各1个时间窗口)
      返回:
      /
    • checkTOTP

      public void checkTOTP(String secretKey, String code, int timeWindowOffset)
      校验用户输入的TOTP是否有效,如果无效则抛出异常
      参数:
      secretKey - Base32编码的密钥
      code - 用户输入的验证码
      timeWindowOffset - 允许的时间窗口偏移量(如1表示允许前后各1个时间窗口)
    • generateGoogleSecretKey

      public String generateGoogleSecretKey(String account)
      生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?secret={secretKey})
      参数:
      account - 账户名
      返回:
      /
    • generateGoogleSecretKey

      public String generateGoogleSecretKey(String account, String secretKey)
      生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?secret={secretKey})
      参数:
      account - 账户名
      secretKey - TOTP 秘钥
      返回:
      /
    • _generateTOTP

      protected String _generateTOTP(String secretKey, long time)