我们决定通过为客户发布 iPhone、Android 和 Blackberry 应用程序的方式开始进行多重身份验证。
Think 的一次性密码系统。
我知道如何生成一个独特的string通过使用基于帐户密钥加上设备序列号(或其他唯一标识符)的 SALT 进行散列。
但是有人知道如何像谷歌那样生成一个独特的、简短的数字吗?和/或有人有关于实现此类事情的文章的良好链接吗?
非常感谢
最后我发现这在RFC 4226 https://www.rfc-editor.org/rfc/rfc4226关于整数转换,可以使用按位运算来完成见第 7 页 https://www.rfc-editor.org/rfc/rfc4226#page-7,本质上它与下面答案中所示的相同。
有stackoverflow 上的另一篇文章 https://stackoverflow.com/questions/4308003/hmac-based-one-time-password-in-c-rfc-4226-hotp关于 C# 上下文中的这一点,如果您处于类似的位置,这可能值得一读。
在 C# 中,我基本上对时间标识符进行了哈希处理(即当前时间(以秒为单位)除以 30 - 得到对当前 30 秒间隔有效的 long)。然后使用我的密钥作为 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 是一个开源项目 - 您可以在这里浏览源代码 http://code.google.com/p/google-authenticator/source/browse/?repo=android.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)