gpt4 book ai didi

python - vb.net 和 python 之间的 hmacsha1 输出十六进制字符串不同

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:52 25 4
gpt4 key购买 nike

我在 vb.net 中使用 HMACSHA1 在 Web 请求中进行消息身份验证。从服务器收到“(403) Forbidden”消息后,我决定看看 VB 中的签名计算是否与给出的示例 Python 代码中的计算相匹配。十六进制字符串非常相似,但并不完全相同。请注意 py 签名中的两个额外 0,但下面 * 指示的 vb 中没有。我假设 py 示例代码很好并且返回正确的输出,因此让 vb 匹配可以解决我的问题,那么差异是什么?

vb sig returns: a729e16e5c4a444a302a72c3d44544fe58aa90
py sig returns: a729e16e5c4a444a3002a72c3d44544f0e58aa90
.................................*..............*.......

这是基于 ( http://apiaxle.com/docs/signing-requests/ ) 处伪代码的 py 代码:

import hmac
from hashlib import sha1
from time import time
key = 'ba1d92...' #32 bit hex number (string)
secret = '2a3759...' #128 bit hex number (string)
curr_time = str(int(time()))
concat = curr_time+key
# hmac expects byte, so convert
concatB = (concat).encode('utf-8')
secretB = secret.encode('utf-8')
h1 = hmac.new(secretB, concatB, sha1)
# h1 is byte, so convert to hex
api_sig = h1.hexdigest()

这是 vb 代码:

Dim uTime As Integer = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
Dim api_sig As String = ""
Using myhmac As New HMACSHA1(System.Text.Encoding.UTF8.GetBytes(secret))
Dim hashValue As Byte() = myhmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(String.Concat(uTime.ToString, key)))
Dim i As Integer
For i = 0 To (hashValue.Length - 1)
api_sig = api_sig + Hex(hashValue(i))
Next i

最佳答案

您的 VB 输出无法正确填充小于 16 的值的十六进制数字;字节 0x02 仅表示为 2,而不是 02,并且 0x0E 字节包含为 e,而不是 0e >.

您需要添加.PadLeft()调用:

api_sig = api_sig + Hex(hashValue(i)).PadLeft(2, "0"c)

或使用字符串格式:

api_sig = api_sig + String.Format("{0:X2}", hashValue(i))

关于python - vb.net 和 python 之间的 hmacsha1 输出十六进制字符串不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com