hmac_sha256在PHP和C#有所不同 [英] hmac_sha256 in php and c# differ
本文介绍了hmac_sha256在PHP和C#有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的PHP代码:
hash_hmac( "sha256", utf8_encode( $filename ), utf8_encode( $password ) );
这是我的C#代码:
and this is my C# code:
var hmacsha256 = new HMACSHA256( Encoding.UTF8.GetBytes( password ) );
hmacsha256.ComputeHash( Encoding.UTF8.GetBytes( filename ) );
不幸的是这两种结果不同。谁能给我一个提示?
unfortunately both results differ. Can anyone give me a hint?
推荐答案
我的C#是不是最好的,但我得到它的工作,你需要做什么是转换的字节数组的结果(十六进制)
My C# is not the best but i got it to work, what you need to do is to convert your byte array results to hex.
PHP
$hash = hash_hmac( "sha256", utf8_encode("Filename"), utf8_encode("Password"));
echo $hash;
// 5fe2ae06ff9828b33fe304545289a3f590bfd948ca9ab731c980379992ef41f1
C#
string password = "Password";
string filename = "Filename";
var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(password));
hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(filename));
foreach(byte test in hmacsha256.Hash)
{
Console.Write(test.ToString("X2"));
}
// 5FE2AE06FF9828B33FE304545289A3F590BFD948CA9AB731C980379992EF41F1
这篇关于hmac_sha256在PHP和C#有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文