如何解决在 C++ 中实现谷歌身份验证器应用程序的问题
我正在尝试在 C++ 中实现 google Authenticator 机制。 我总是得到与谷歌身份验证器不同的 OTP。
我想知道为什么?
1 - 我搜索并发现谷歌使用 HMAC-SHA1 算法。但我很怀疑,因为:
- 我读过的几乎所有文章都是旧的。似乎无关紧要。
- 我发现谷歌身份验证器代码现在是私有的 开源。所以恐怕谷歌可能会改变使用的算法
- 我发现 SHA1 不再安全。有些人设法使 SHA1 冲突。如果谷歌仍然使用它而不是它,我会感到惊讶 其他更安全的算法,如 SHA256、SHA512。
如果您有任何相关信息,请告诉我。
2 - 我确定我的代码是正确的。
-
因为我将输出的哈希值与自动生成的哈希值进行了比较 在这个网站上,它们总是匹配的。 网站:https://www.liavaag.org/English/SHA-Generator/HMAC/
-
因为代码非常简单!
代码如下:
/*****************************************************************************************/
#include <iostream>
#include <cmath>
#include <ctime>
#include <stdio.h>
#include <string>
#include "SHA1.h"
#include "HMAC_SHA1.h"
using namespace std;
void IntegerToString(time_t epochTime,BYTE timeAsstring[],int length);
int TimeLength(time_t epochTime);
int main(void)
{
CHMAC_SHA1 sha1;
int one,two,three,four;
time_t result = time(nullptr);
result = (long long)floor(result/30);
cout << result <<endl;
int offset;
int truncatedHash;
int finalOTP;
BYTE key[33] = "atoqb4y572clkhj2sls5h72a76cnyzpe";
int key_len = 33; /*32 + null terminator*/
int text_len = TimeLength(result);
//cout << text_len << endl;
BYTE text[text_len+1];
IntegerToString(result,text,text_len);
text[text_len] = '\0';
//printf("%s",text);
/*the digest is always 20 bytes long*/
BYTE digest[20] = {0};
sha1.HMAC_SHA1(text,text_len,key,key_len-1,digest);
printf("0x");
for(int i = 0 ; i < 20 ; i++)
{
printf("%x",(int)digest[i]);
}
offset = digest[19] & 0xf;
one = (digest[offset+0] & 0x7f) << 24;
two = (digest[offset+1] & 0xff) << 16;
three = (digest[offset+2] & 0xff) << 8;
four = (digest[offset+3] & 0xff);
truncatedHash = one | two | three | four;
finalOTP = (truncatedHash % (1000000));
cout << "\nyour OTP : " << finalOTP << endl;
return 0;
}
void IntegerToString(time_t epochTime,int length)
{
time_t reverse = 1; /*to hold zeros*/
int i = 0;
while(epochTime >= 1) /*reverse : 1000 -> 10001*/
{
reverse = reverse*10+epochTime%10;
epochTime = epochTime / 10;
//cout << epochTime << "*" << reverse <<endl;
}
while(reverse != 1)
{
timeAsstring[i] = (reverse%10 +'0');
reverse = reverse / 10;
//cout << i << timeAsstring[i] << "#" << reverse << endl;
i++;
}
}
int TimeLength(time_t epochTime)
{
int counter = 0;
while(epochTime)
{
epochTime = epochTime / 10;
counter++;
}
return counter;
}
请帮忙。
解决方法
您使用的密钥“atoqb4y572clkhj2sls5h72a76cnyzpe”似乎不是十六进制格式。 您应该使用十六进制(字符串的十六进制是“04dd00f31dfe84b51d3a92e5d3ff40ff84dc65e4”)。如果需要使用base32,需要先转成hex
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。