我们已决定通过为我们的客户发布 iPhone、Android 和 Blackberry 应用程序来开始进行多因素身份验证。
思考Google Authenticator的一次性密码系统。
我可以了解如何通过使用基于帐户 key 加上设备序列号(或其他唯一标识符)的 SALT 进行散列来生成唯一的字符串。
但是有没有人知道如何像 google 那样生成唯一的短号码?和/或是否有人有关于实现此类目标的文章的良好链接?
非常感谢
最佳答案
最后我发现这在 RFC 4226 中有很好的记录。关于整数转换,这可以使用按位运算来完成 shown on page 7 ,基本上它与下面的答案中显示的相同。
有another post on stackoverflow关于 C# 上下文中的这一点,如果您处于类似的位置,这可能值得一读。
在 C# 中,我基本上对时间标识符进行哈希处理(即当前时间(以秒为单位)除以 30 - 以获得对当前 30 秒间隔有效的 long)。然后使用我的 key 作为 SALT 对其进行哈希处理。
然后……
// Use a bitwise operation to get a representative binary code from the hash
// Refer section 5.4 at https://www.rfc-editor.org/rfc/rfc4226#page-7
int offset = hashBytes[19] & 0xf;
int binaryCode = (hashBytes[offset] & 0x7f) << 24
| (hashBytes[offset + 1] & 0xff) << 16
| (hashBytes[offset + 2] & 0xff) << 8
| (hashBytes[offset + 3] & 0xff);
// Generate the OTP using the binary code. As per RFC 4426 [link above] "Implementations MUST extract a 6-digit code at a minimum
// and possibly 7 and 8-digit code"
int otp = binaryCode % (int)Math.Pow(10, 6); // where 6 is the password length
return otp.ToString().PadLeft(6, '0');
对于那些不知道的人,Google Authenticator 是一个开源项目 - 您可以 browse the source code here .
关于c# - 如何生成一次性密码 (OTP/HOTP)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169253/