使用SHA256散列密码和.NET / Node.js的 [英] Hashing a password using SHA256 and .NET/Node.js

查看:265
本文介绍了使用SHA256散列密码和.NET / Node.js的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林SHA256存储在我的数据库用户口令由.NET产生的哈希值,我需要能够与node.js中检查他们唯一的问题是,.NET和Node.js的创建不同的哈希密码相同。

Im Storing SHA256 hashes of user passwords in my database generated by .NET and I need to be able to check them with Node.js. The only problem is that .NET and Node.js create different hashes for the same password.

Password: ThisPassword  

.NET:

var ue = new UnicodeEncoding();  
var byteSourceText = ue.GetBytes("ThisPassword");  
var byteHash = new System.Security.Cryptography.SHA256Managed().ComputeHash(byteSourceText);  
return Convert.ToBase64String(byteHash);

//Tlwxyd7HIQhXkN6DrWJtmB9Ag2fz84P/QgMtbi9XS6Q=

的Node.js(使用加密):

var crypto = require('crypto');
return crypto.createHash('sha256').update('ThisPassword').digest('base64')

//d7I986+YD1zS6Wz2XAcDv2K8yw3xIVUp7u/OZiDzhSY=

我发现<一href="http://stackoverflow.com/questions/10559203/sha-256-hashes-different-between-c-sharp-and-javascript">this,但无法弄清楚如何实施他的解决方案。

I found this, but was unable to figure out how to implement his solution.

推荐答案

编辑:您是在C#中使用UTF-16,则必须使用相同的编码在两种语言:

You are using UTF-16 in C#, you must use same encoding in both languages:

Node.js的:

var crypto = require("crypto");
var sha256 = crypto.createHash("sha256");
sha256.update("ThisPassword", "utf8");//utf8 here
var result = sha256.digest("base64");
console.log(result); //d7I986+YD1zS6Wz2XAcDv2K8yw3xIVUp7u/OZiDzhSY=

C#:

SHA256 sha256 = SHA256Managed.Create(); //utf8 here as well
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes("ThisPassword"));
string result = Convert.ToBase64String(bytes);
Console.WriteLine(result); //d7I986+YD1zS6Wz2XAcDv2K8yw3xIVUp7u/OZiDzhSY=

这篇关于使用SHA256散列密码和.NET / Node.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆