0

In C# I have the following code:

public static string GetHashCode(string p)
    {
        var a = new SHA256Managed();
        return Convert.ToBase64String(a.ComputeHash(new System.Text.UTF8Encoding().GetBytes(p)));
    }

And I have to achieve the same thing in Javascript, I was trying the following but it gives different result:

btoa((CryptoJS.SHA256(this.toUTF8Array(this.settingsService.Password)).toString(CryptoJS.enc.Hex)));

What am I supposed to do, what is wrong?

Example output for string 'aaa' for: C#:1f9e1d76685d765aa3a6ff85dced2f0a04f612536df52696684aaa67787e6cdd Js:NGVhNWM1MDhhNjU2NmU3NjI0MDU0M2Y4ZmViMDZmZDQ1Nzc3N2JlMzk1NDljNDAxNjQzNmFmZGE2NWQyMzMwZQ==

1 Answers1

3

I'm not that familiar with CryptoJS but I think you are outputting hex which is not the same as base64.

This generates the same result as C# see fiddle

var utf8arr = CryptoJS.enc.Utf8.parse("apassword");
var hash = CryptoJS.SHA256(utf8arr);
var base64 = CryptoJS.enc.Base64.stringify(hash);
console.log(base64);
Crowcoder
  • 11,250
  • 3
  • 36
  • 45