我需要在C#中转换以下php代码:
$res = mac256($ent, $key);
$result = encodeBase64($res);
Run Code Online (Sandbox Code Playgroud)
哪里
function encodeBase64($data)
{
$data = base64_encode($data);
return $data;
}
Run Code Online (Sandbox Code Playgroud)
和
function mac256($ent,$key)
{
$res = hash_hmac('sha256', $ent, $key, true);//(PHP 5 >= 5.1.2)
return $res;
}
Run Code Online (Sandbox Code Playgroud)
我使用以下C#代码:
byte[] res = HashHMAC(ent, key);
string result = System.Convert.ToBase64String(res);
Run Code Online (Sandbox Code Playgroud)
哪里
public byte[] HashHMAC(string ent, byte[] key)
{
byte[] toEncryptArray =System.Text.Encoding.GetEncoding(28591).GetBytes(ent);
HMACSHA256 hash = new HMACSHA256(key);
return hash.ComputeHash(toEncryptArray);
}
Run Code Online (Sandbox Code Playgroud)
这个链接提供完整的php源代码
我也在php中检查了这篇文章hmac_sha256和c#的区别
但结果却不尽相同.