【发布时间】:2018-04-03 23:02:33
【问题描述】:
我有一个在 .net 核心网站中使用的数据库,现在我需要使用节点 js 检查登录 如何检查 node.js 中的密码哈希,我找到了一个 function 这样做但不适用于 .net 核心 AspNetUsers,只能在最后一个 asp.net 中使用
【问题讨论】:
标签: node.js asp.net-core
我有一个在 .net 核心网站中使用的数据库,现在我需要使用节点 js 检查登录 如何检查 node.js 中的密码哈希,我找到了一个 function 这样做但不适用于 .net 核心 AspNetUsers,只能在最后一个 asp.net 中使用
【问题讨论】:
标签: node.js asp.net-core
node-password-hash(尽管它已被弃用)是一个简单/出色的模块,它可以通过简单的步骤为您完成密码散列。
它还提供了生成散列密码和根据散列密码验证纯文本密码的功能。除此之外,作为一项额外的安全措施,在对密码进行哈希处理时会生成一个随机盐。散列密码是使用的加密算法和盐的组合,因此您只需验证纯文本密码就是散列密码本身。
希望这会有所帮助!
【讨论】:
作为这篇文章:JavaScript: How to generate Rfc2898DeriveBytes like C#?
但是对于 .net core 需要更改,因为在 .net core ^1 中用于散列使用 v3,所以代码应该更改为:
// The value stored in [dbo].[AspNetUsers].[PasswordHash]
const hashedPwd = "AQAAAAEAACcQAAAAENX1Hdhgta05DYzYzVOI5kfv1mM0oc2OCIF8tKvNZeSTMWoczGZk+6yy9DMWtLeVQQ==";
const hashedPasswordBytes = new Buffer(hashedPwd, 'base64');
const hexChar = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
let salt_string = "";
let storedSubKeyString = "";
// build strings of octets for the salt and the stored key
for (let i = 1; i < hashedPasswordBytes.length; i++) {
if (i > 12 && i <= 28) {
salt_string += hexChar[(hashedPasswordBytes[i] >> 4) & 0x0f] + hexChar[hashedPasswordBytes[i] & 0x0f]
}
if (i > 0 && i > 28) {
storedSubKeyString += hexChar[(hashedPasswordBytes[i] >> 4) & 0x0f] + hexChar[hashedPasswordBytes[i] & 0x0f];
}
}
// password provided by the user
const password = 'password';
var nodeCrypto = crypto.pbkdf2Sync(
new Buffer(password),
new Buffer(salt_string, 'hex'), 10000, 256, 'SHA256');
var derivedKeyOctets = nodeCrypto.toString('hex').toUpperCase();
if (derivedKeyOctets.indexOf(storedSubKeyString) === 0) {
return "passwords match!";
} else {
return "passwords DO NOT match!";
}
【讨论】: