基于时间(20 秒有效期)的谷歌验证码,我需要在读取 4 位数代码之前检查时间。
- 使用 TOTP 收集 google auth 代码
- 在我们的应用程序中自动应用代码
问题,在读取边缘的代码(18/19 秒),并自动将代码发送到我们的文本框,但有效性已过期且身份验证失败。所以我想检查代码以及有效期
一个。如果有效时间大于 10 秒,我可以获得代码并将其传递给文本框 b。如果有效期小于10秒,等待10秒
代码:
public static String getTOTPCode(String secretKey) {
String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase();
Base32 base32 = new Base32();
byte[] bytes = base32.decode(normalizedBase32Key);
String hexKey = Hex.encodeHexString(bytes);
return TOTP.getOTP(hexKey);
}
jar 文件
commons-code1.8 jar
totp-1.0 jar
参考上面,让我知道如何获得OTP的有效时间?