1
我在vb.net中使用HMACSHA1進行webrequest中的消息身份驗證。從服務器獲得「(403)Forbidden」消息後,我決定查看我在VB中的簽名計算是否與我給出的示例python代碼中的計算匹配。十六進制字符串非常相似,但不完全一樣。請注意py簽名中的兩個額外的0,但不包含在*所示的vb中。我假設py示例代碼是好的,並返回正確的輸出,所以讓vb匹配可以解決我的問題,那麼什麼是差異?hmacsha1在vb.net和python之間輸出十六進制字符串不同
vb sig returns: a729e16e5c4a444a302a72c3d44544fe58aa90
py sig returns: a729e16e5c4a444a3002a72c3d44544f0e58aa90
.................................*..............*.......
這裏是PY代碼,基於在(http://apiaxle.com/docs/signing-requests/)僞代碼:
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