0

I'm trying to convert this function from C# to node but I'm getting different results when I try to convert a buffer to a string.

string str = "34924979";
System.Security.Cryptography.SHA512 sha = new System.Security.Cryptography.SHA512Managed();
byte[] ba = System.Text.Encoding.ASCII.GetBytes(str);
byte[] data= sha.ComputeHash(ba);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(data));


>> `?????gV)????∟?Z?s??v$We-??N?"?w????0??\i♠G???

that's what i'm trying to do.

const crypto = require('crypto')

const str = '34924979';
const sha = crypto.createHash('sha512').update(str).digest('utf8');
const hash = sha.toString('utf-8').replace(/\uFFFD/g, '?');
console.log(hash);

>> `????gV)????∟?Z?v$We-??N?"?w????0?\i♠Gߕ?
1
  • 2
    Well for one... you use ASCII in C# and UTF-8 in Node.js... Commented Jan 5, 2023 at 15:05

1 Answer 1

2

You're using different encodings in C# and JS.

Try changing the JS code to the following:

const sha = crypto.createHash('sha512').update(str).digest('ascii');
const hash = sha.toString('ascii');
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and got the same result.
Needed to update the digest method parameter on the line above as well - updated answer
The ascii parameter in digest solved my issue. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.