0

How i can do below JS in C#?

var contentHash = CryptoJS.SHA512(Json.stringify(requestBody)).toString(CryptoJS.enc.Hex)

Try below code but not working.

{
    var encoding = new UTF8Encoding();

    var messageBytes = encoding.GetBytes(str);
    var keyBytes = encoding.GetBytes(cypherkey);

    using var hmacsha256 = new HMACSHA256(keyBytes);
    var hashmessage = hmacsha256.ComputeHash(messageBytes);
        
    var myStr= Convert.ToBase64String(hashmessage);
        
    return myStr;
}
David
  • 208,112
  • 36
  • 198
  • 279
EllisK
  • 1
  • 2
  • Welcome to Stack Overflow! In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Nov 02 '22 at 11:35
  • 3
    The original code is doing `SHA512`. Why would you think `HMACSHA256` is comparable? – Jeroen Mostert Nov 02 '22 at 11:35
  • base64 encoding is not Hex, see this answer for HEX representation out of a string https://stackoverflow.com/a/16999640/1927444 – Pablo Recalde Nov 02 '22 at 11:35
  • also @JeroenMostert is right, HMAC classes have a different purpose. Take a look at https://learn.microsoft.com/es-es/dotnet/api/system.security.cryptography.sha256?view=net-6.0 – Pablo Recalde Nov 02 '22 at 11:38

0 Answers0