Home > Enterprise >  Equivalent from PHP hash_hmac("sha384", $data, $key) in .NET
Equivalent from PHP hash_hmac("sha384", $data, $key) in .NET

Time:06-03

In PHP i do following steps:

<?php
  $key="mM8Y28b5R7KFe3Y5";   
  $data="298052380sbEUREinzellizenz Spieler SEN329739test120211572380R-00001003authorization19";
  $hash=hash_hmac("sha384", $data, $key);
  echo $hash;
?>

i get following output:

f34fb28f6462cde18a92ee854d289ba5aed3cfc07d0abb52cfef3b70028e1b38b098ae17514a9121d43d3d6f684eccd8

I try to do this in .NET with:

string data = "298052380sbEUREinzellizenz Spieler SEN329739test120211572380R-00001003authorization19";
      string key = "mM8Y28b5R7KFe3Y5";
      string hash = "";
      using (SHA384 sha384Hash = SHA384.Create())
      {
        byte[] sourceBytes = Encoding.ASCII.GetBytes(data);
        byte[] hashBytes = sha384Hash.ComputeHash(sourceBytes);
        hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToLower();
      }

and i get

77b7eccd4f66b07c66e2c49b83e53bf84dfd4dc67ec60007df4476adfe16d9b0bfdd6e58e9a5008bde4908335d161ef5

i tried also:

var keyByte = Encoding.ASCII.GetBytes(key);
      using (HMACSHA256 sha384Hash = new HMACSHA256(keyByte))
      {
        byte[] sourceBytes = Encoding.ASCII.GetBytes(data);
        byte[] hashBytes = sha384Hash.ComputeHash(sourceBytes);
        hash2 = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToLower();
      }

hash2 is

ceaabe254e19d77940ac3edfdf2fa3d1de424d569136e8deb770aa81be5cb24a

i don't get the difference. I checked already the Encodings, but i see no differents.

What i am doing wrong?

CodePudding user response:

use the second example but with

using (HMACSHA384 sha384Hash = new HMACSHA384(Encoding.ASCII.GetBytes(key)))
  • Related