在nodejs中创建类似System.Web.Helpers.Crypto.HashPassword(ASP.NET)的方法?

Create method like System.Web.Helpers.Crypto.HashPassword (ASP.NET) in nodejs?

如何在 nodejs 中像 https://docs.microsoft.com/en-us/previous-versions/aspnet/web-frameworks/gg538287(v=vs.111) 一样使用 RFC 2898 生成密码哈希?

我的 nodejs 应用程序正在使用 table of SQL 服务器,其密码字段由 Crypto.HashPassword of ASP.NET 散列,因此我需要在 nodejs 中创建相同的函数以比较一下。

const crypto = require('crypto');
const hexChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
const verifyHashedPassword = (password, hashedPwd) => {
  let saltString = '';
  let storedSubKeyString = '';
  const hashedPasswordBytes = new Buffer(hashedPwd, 'base64');
  for (var i = 1; i < hashedPasswordBytes.length; i++) {
    if (i > 0 && i <= 16) {
      saltString += hexChar[(hashedPasswordBytes[i] >> 4) & 0x0f] + hexChar[hashedPasswordBytes[i] & 0x0f];
    }
    if (i > 0 && i > 16) {
      storedSubKeyString += hexChar[(hashedPasswordBytes[i] >> 4) & 0x0f] + hexChar[hashedPasswordBytes[i] & 0x0f];
    }
  }
  const nodeCrypto = crypto.pbkdf2Sync(new Buffer(password), new Buffer(saltString, 'hex'), 1000, 256, 'sha1');
  const derivedKeyOctets = nodeCrypto.toString('hex').toUpperCase();
  return derivedKeyOctets.indexOf(storedSubKeyString) === 0;
};

我用它来比较纯密码和散列密码。效果不错!