我在将C#散列算法与节点匹配时遇到问题,问题似乎是Unicode编码.有没有办法将字符串转换为Unicode然后散列它并输出为十六进制?不幸的是我不能改变c#代码,这是我无法控制的.
节点算法
function createMd5(message) {
var crypto = require("crypto");
var md5 = crypto.createHash("md5");
return md5.update(message).digest('hex');
}
Run Code Online (Sandbox Code Playgroud)
c#散列算法
private static string GetMD5(string text)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] hashValue;
byte[] message = UE.GetBytes(text);
using (MD5 hasher = new MD5CryptoServiceProvider())
{
string hex = "";
hashValue = hasher.ComputeHash(message);
foreach (byte x in hashValue)
{
hex += String.Format("{0:x2}", x);
}
return hex.ToLower();
}
}
Run Code Online (Sandbox Code Playgroud)
你怀疑这是一个编码问题是正确的.您可以使用以下更改来修复节点代码,这会将您的消息字符串转换为utf-16(这是.NET的默认编码):
function createMd5(message) {
var crypto = require("crypto");
var md5 = crypto.createHash("md5");
return md5.update(new Buffer(message, 'ucs-2')).digest('hex');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1389 次 |
| 最近记录: |