从缓冲区到字符串的转换在c#和Nodejs中会产生不同的结果

anauzrmj  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(103)

我尝试将这个函数从C#转换为节点,但是当我尝试将缓冲区转换为字符串时,我得到了不同的结果。

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???

这就是我想做的。

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ߕ?
eh57zj3b

eh57zj3b1#

您在C#和JS中使用了不同的编码。
尝试将JS代码更改为以下代码:

const sha = crypto.createHash('sha512').update(str).digest('ascii');
const hash = sha.toString('ascii');

相关问题