在C#和Node.js中,从缓冲区转换为字符串会产生不同的结果。

huangapple go评论77阅读模式
英文:

Conversion from buffer to string gives different results in c# and Nodejs

问题

Here is the translated code snippet:

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);

This code is the Node.js equivalent of the C# code you provided for converting a string to a SHA-512 hash and handling the output as a string.

英文:

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

你在C#和JS中使用了不同的编码。

尝试将JS代码更改为以下内容:

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

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');

huangapple
  • 本文由 发表于 2023年1月5日 23:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75020470.html
  • buffer
  • c#
  • javascript
  • node.js

为所有缺失的枚举添加到List中。 go 54
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码