我想從ankoder.com測試API,並且在authentication token的摘要計算上有問題。當我嘗試從C#調用時,示例是紅寶石。當我比較HMAC-SHA1之間的摘要結果時,我遇到了密鑰結果的問題。紅寶石hmac-sha1不同於C#HMACSHA1
爲了方便在這裏測試的是代碼:
require 'hmac-sha1'
require 'digest/sha1'
require 'base64'
token="-Sat, 14 Nov 2009 09:47:53 GMT-GET-/video.xml-"
private_key="whatever"
salt=Digest::SHA1.hexdigest(token)[0..19]
passkey=Base64.encode64(HMAC::SHA1.digest(private_key, salt)).strip
這使我的結果是: 「X/0EngsTYf7L8e7LvoihTMLetlM = \ n」 個 如果我嘗試在C#與以下:
const string PrivateKey = "whatever";
var date = "Sat, 14 Nov 2009 09:47:53 GMT";//DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
string token=string.Format("-{0}-GET-/video.xml-", date);
var salt_binary=SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(token));
var salt_hex=BitConverter.ToString(salt_binary).Replace("-", "").ToLower();
var salt =salt_hex.Substring(0,20);
var hmac_sha1 =
new HMACSHA1(Encoding.ASCII.GetBytes(salt));
hmac_sha1.Initialize();
var private_key_binary = Encoding.ASCII.GetBytes(PrivateKey);
var passkey_binary = hmac_sha1.ComputeHash(private_key_binary,0,private_key_binary.Length);
var passkey = Convert.ToBase64String(passkey_binary).Trim();
鹽結果是相同的,但密鑰的結果不同 - C#給我:
QLC68XjQlEBurwbVwr7euUfHW/K =
兩者都產生鹽:f5cab5092f9271d43d2e
任何好主意發生了什麼?
只是想知道,你會得到什麼結果呢? – quantumpotato
X/0EngsTYf7L8e7LvoihTMLetlM = –