Package cn.dev33.satoken.secure.totp
Class SaTotpTemplate
java.lang.Object
cn.dev33.satoken.secure.totp.SaTotpTemplate
TOTP 算法类,支持 生成/验证 动态一次性密码
- Since:
- 1.42.0
- Author:
- click33
-
Field Summary
FieldsModifier and TypeFieldDescriptionint生成的验证码位数哈希算法(HmacSHA1、HmacSHA256等)int密钥长度(字节,推荐16或32)int时间窗口步长(秒) -
Constructor Summary
ConstructorsConstructorDescription构造函数 (使用默认参数)SaTotpTemplate(int timeStep, int codeDigits, String hmacAlgorithm, int secretKeyLength) 构造函数 (使用自定义参数) -
Method Summary
Modifier and TypeMethodDescription_generateTOTP(String secretKey) 生成当前时间的 TOTP 验证码protected String_generateTOTP(String secretKey, long time) void校验用户输入的TOTP是否有效,如果无效则抛出异常generateGoogleSecretKey(String account) 生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?generateGoogleSecretKey(String account, String secretKey) 生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?generateGoogleSecretKey(String account, String issuer, String secretKey) 生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{issuer}:{account}?生成随机密钥(Base32编码)booleanvalidateTOTP(String secretKey, String code, int timeWindowOffset) 判断用户输入的 TOTP 是否有效
-
Field Details
-
timeStep
public int timeStep时间窗口步长(秒) -
codeDigits
public int codeDigits生成的验证码位数 -
hmacAlgorithm
哈希算法(HmacSHA1、HmacSHA256等) -
secretKeyLength
public int secretKeyLength密钥长度(字节,推荐16或32)
-
-
Constructor Details
-
SaTotpTemplate
public SaTotpTemplate()构造函数 (使用默认参数) -
SaTotpTemplate
构造函数 (使用自定义参数)- Parameters:
timeStep- 时间窗口步长(秒)codeDigits- 生成的验证码位数hmacAlgorithm- 哈希算法(HmacSHA1、HmacSHA256等)secretKeyLength- 密钥长度(字节,推荐16或32)
-
-
Method Details
-
generateSecretKey
生成随机密钥(Base32编码)- Returns:
- /
-
_generateTOTP
生成当前时间的 TOTP 验证码- Parameters:
secretKey- Base32 编码的密钥- Returns:
- /
-
validateTOTP
判断用户输入的 TOTP 是否有效- Parameters:
secretKey- Base32编码的密钥code- 用户输入的验证码timeWindowOffset- 允许的时间窗口偏移量(如1表示允许前后各1个时间窗口)- Returns:
- /
-
checkTOTP
校验用户输入的TOTP是否有效,如果无效则抛出异常- Parameters:
secretKey- Base32编码的密钥code- 用户输入的验证码timeWindowOffset- 允许的时间窗口偏移量(如1表示允许前后各1个时间窗口)
-
generateGoogleSecretKey
生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?secret={secretKey})- Parameters:
account- 账户名- Returns:
- /
-
generateGoogleSecretKey
生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{account}?secret={secretKey})- Parameters:
account- 账户名secretKey- TOTP 秘钥- Returns:
- /
-
generateGoogleSecretKey
生成谷歌认证器的扫码字符串 (形如:otpauth://totp/{issuer}:{account}?secret={secretKey}&issuer={issuer})- Parameters:
account- 账户名issuer- 签发者secretKey- TOTP 秘钥- Returns:
- /
-
_generateTOTP
-