我需要将以下PHP代码转换为C#代码:
$hash = hash_hmac('SHA256',"ent",'key',true);
echo rtrim(strtr(base64_encode($hash), '+/', '-_'), '=');
我使用以下C#代码:(FROM HERE)
private static string hmacSHA256(String data, String key)
{
using (HMACSHA256 hmac = new HMACSHA256(Encoding.ASCII.GetBytes(key))) {
byte[] a2 = hmac.ComputeHash(Encoding.ASCII.GetBytes(data));
string a3 = Convert.ToBase64String(a2).ToString.Replace("+", "-").Replace("/", "_").Replace("=", "");
return a3;
}
}
但结果并不相同。
PHP的结果:Xt0hGF_wcArzx4urxfbHUpOp2eEXvtGTDekGtQw5JTo
C#的结果:FnM4gpRRlapTZcO5iAIlEXEqU5iFT1hYcmQ1rY7ZINE