我试着翻译TOTP到VB.Net一个JavaScript应用程序之间的不同的输出:http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/HMACSHA1给JS和VB.Net
我见HMAC-部分的翻译过程中遇到的一个问题:
//Javascript:
var hmacObj = new jsSHA("Hello World!", 'HEX');
var hmac = hmacObj.getHMAC("secret", 'HEX', 'SHA-1', "HEX");
这是我翻译的在VB.Net一codesnippet
'VB.Net:
Dim hmacObjTest As New HMACSHA1(System.Text.Encoding.UTF8.GetBytes("secret"))
Dim hmacTest As Byte() = hmacObjTest.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Hello World!"))
Dim hmacHexTest As New StringBuilder()
For i As Integer = 0 To hmacTest.Length - 1
hmacHexTest.Append(hmacTest(i).ToString("x2"))
Next i
Dim strTest As String = "HMAC = " & hmacHexTest.ToString()
的问题是,我从两种语言不同的输出:
Output JS: 5efed98b0787c83f9cb0135ba283c390ca49320e //Tested from jsSha demo: http://caligatio.github.io/jsSHA/
Output VB.Net: 87b0154b8420c0b58869ca103f481e824d8876ea
输出是不是都一样像他们这样一个问题:hmacsha1 output hex strings different between vb.net and python
有谁知道我可能是做错了什么?
谢谢你的回答。 JavaScript代码具有我正在寻找的输出,所以我不需要更改该代码。你知道JavaScript的getHMAC中使用的编码标准吗?任何方式改为在VB.Net正确的标准呢? – Encrypto94
使用的编码由'System.Text.Encoding.UTF8.GetBytes()'定义,在这种情况下显然是UTF-8。你可以在这里使用你选择的编码。 –