Package org.aoju.bus.crypto.digest.otp
Class TOTP
java.lang.Object
org.aoju.bus.crypto.digest.otp.HOTP
org.aoju.bus.crypto.digest.otp.TOTP
time-based one-time passwords (TOTP) 基于时间戳算法的一次性密码生成器, 规范见:RFC 6238
时间同步,基于客户端的动态口令和动态口令验证服务器的时间比对,一般每30秒产生一个新口令, 要求客户端和服务器能够十分精确的保持正确的时钟,客户端和服务端基于时间计算的动态口令才能一致
参考:https://github.com/jchambers/java-otp
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsFields inherited from class org.aoju.bus.crypto.digest.otp.HOTP
DEFAULT_PASSWORD_LENGTH -
Constructor Summary
ConstructorsConstructorDescriptionTOTP(byte[] key) 构造,使用默认HMAC算法(HmacSHA1)构造,使用默认HMAC算法(HmacSHA1)构造,使用默认HMAC算法(HmacSHA1)构造 -
Method Summary
Modifier and TypeMethodDescriptionint使用给定的时间戳生成一次性密码static StringgenerateGoogleSecretKey(String account, int numBytes) 生成谷歌认证器的字符串(扫码字符串) 基于时间的,计数器不适合获取步进boolean用于验证code是否正确Methods inherited from class org.aoju.bus.crypto.digest.otp.HOTP
generate, generateSecretKey, getAlgorithm, getPasswordLength
-
Field Details
-
DEFAULT_TIME_STEP
默认步进 (30秒)
-
-
Constructor Details
-
TOTP
public TOTP(byte[] key) 构造,使用默认HMAC算法(HmacSHA1)- Parameters:
key- 共享密码,RFC 4226要求最少128位
-
TOTP
构造,使用默认HMAC算法(HmacSHA1)- Parameters:
timeStep- 日期步进,用于生成移动因子(moving factor)key- 共享密码,RFC 4226要求最少128位
-
TOTP
构造,使用默认HMAC算法(HmacSHA1)- Parameters:
timeStep- 日期步进,用于生成移动因子(moving factor)passwordLength- 密码长度,可以是6,7,8key- 共享密码,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,8algorithm- HMAC算法枚举key- 共享密码,RFC 4226要求最少128位
-
-
Method Details
-
generateGoogleSecretKey
生成谷歌认证器的字符串(扫码字符串) 基于时间的,计数器不适合- Parameters:
account- 账户名numBytes- 将生成的种子字节数量- Returns:
- 共享密钥
-
generate
使用给定的时间戳生成一次性密码- Parameters:
timestamp- 用于生成密码的时间戳- Returns:
- 一次性密码的int形式
-
validate
用于验证code是否正确- Parameters:
timestamp- 验证时间戳offsetSize- 误差范围code- code- Returns:
- 是否通过
-
getTimeStep
获取步进- Returns:
- 步进
-