我正在尝试实现TOTP,但是我遇到了一个问题,因为的令牌输出和这里计算的结果不匹配。
开发环境:Java(Servlet) Eclipse
我使用使用key.getKey ()从用户名生成密钥,并使用Base32 ()对其进行编码。
最终的GoogleAuthenticatorKey key = gAuth.createCredentials( userName );key.getKey()使用Apache Base32()
生成QR代码。也见图.

log : Token = 723025
使用基于密钥的SHA256算法生成哈希。
日志: HS = HMAC-SHA-1( K,C):K= IU3U2U2SINBEQWSWJNCUINC2KVLEMSKYLFLFCNRUJ5JTMS2RLBKQ变量:K= secretKey SecretKeySpec sks =新SecretKeySpec( secretKey,HMAC_SHA256 );Mac = Mac.getInstance( HMAC_SHA256 );mac.init(sks);byte[] hmacBytes = mac.doFinal();new;
生成的哈希如下所示。
变量: HS = hmacBytes HS =hmacBytes
另见: RFC 4226 - HOTP
散列长度为32。log : hash.length = 32 Hash,获取32字节的下4位。HS = 3c72f2b3802ab9b8a210ea992c436683ddd73c6a9cf55bfcb30921e7b359e1e 4
在这种情况下,偏移量是4。
日志:偏移量=4
从哈希的第四个字节中获取4个字节。
HS = 3c72f2b3 802ab9b8 a210ea992c436683ddd73c6a9cf55bfcb30921e7b359e1e4日志:e=8 0 2 a b9 b8
或者每个字节。
long e0 = << 28;long # e6 20= numeric1 << 24;long e2 = numeric2 << 20;long e3 = numeric3 << 16;long e4 = numeric4 << 12;long e5 = numeric5 << 8;long << = 4;long = 0;// = 8,= 0,= 2,= a,= b,= 9,= b,# e6 30=8长截尾(en32 en34 en36 en38#en38;en36 en38#en38]
我会把源代码放在热情洋溢的线上。
* // hotp为数字=6 // D= Snum模10 ^6长pow = ( long )Math.pow( 10,6 );long HOTP = (long)(截断% pow );//零按下字符串totp = String.format( "%06d",(int)hotp );
令牌=723025(来自)结果= 514302(this)

发布于 2021-07-16 20:44:16
你为什么要生成一个新哈希呢?您应该只使用IU3U2U2SINBEQWSWJNCUINC2KVLEMSKYLFLFCNRUJ5JTMS2RLBKQ (可能需要转换为十六进制)
https://stackoverflow.com/questions/68374934
复制相似问题