php ve c # hmac_sha256 farklılık

1 Cevap

Bu benim PHP kodu:

hash_hmac( "sha256", utf8_encode( $filename ), utf8_encode( $password ) );

ve bu benim C # kodu:

var hmacsha256 = new HMACSHA256( Encoding.UTF8.GetBytes( password ) );
hmacsha256.ComputeHash( Encoding.UTF8.GetBytes( filename ) );

ne yazık ki hem sonuçlar farklı. Herkes bana bir ipucu verebilir misiniz?

1 Cevap

Benim C # iyi değil ama ben ne yapmanız gereken hex için bayt dizisi sonuçları dönüştürmek için, çalışmak lazım.

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