![](/img/trans.png)
[英]Conversion from buffer of bytes to string then back to bytes in NodeJS / Javascript
[英]Conversion from buffer to string gives different results in c# and Nodejs
我正在嘗試將此 function 從 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ߕ?
您在 C# 和 JS 中使用了不同的編碼。
嘗試將 JS 代碼更改為以下內容:
const sha = crypto.createHash('sha512').update(str).digest('ascii');
const hash = sha.toString('ascii');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.