asp.net - 如何在 node.js 中检查 ASP.NET 密码哈希

标签 asp.net node.js algorithm hash

首先我读了这个Hashing a password using SHA256 and .NET/Node.js这对我没有帮助。

我必须在 node.js 环境中验证在 ASP.NET 中创建的密码哈希值。有人告诉我密码是使用以下算法生成的:What is default hash algorithm that ASP.NET membership uses? .

我有示例密码哈希和盐(第一行是密码,第二行是盐):

"Password": "jj/rf7OxXM263rPgvLan4M6Is7o=",
"PasswordSalt": "/Eju9rmaJp03e3+z1v5s+A==",

我知道哈希算法是 SHA1 并且我知道上面的哈希是为输入 test123 生成的。但是我无法重现哈希算法以获得此输入的相同哈希值。我尝试了什么:

Password = "jj/rf7OxXM263rPgvLan4M6Is7o="
PasswordSalt = "/Eju9rmaJp03e3+z1v5s+A=="
crypto = require("crypto")
sha1 = crypto.createHash("sha1")
PasswordSalt = new Buffer(PasswordSalt, 'base64').toString('utf8')
sha1.update(PasswordSalt+"test123", "utf8")
result = sha1.digest("base64")
console.log(Password)
console.log(result)

结果是:

jj/rf7OxXM263rPgvLan4M6Is7o=
xIjxRod4+HVYzlHZ9xomGGGY6d8=

我能够使用 C# 算法:

using System.IO;
using System;
using System.Text;
using System.Security.Cryptography;

class Program
{

    static string EncodePassword(string pass, string salt)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(pass);
        byte[] src = Convert.FromBase64String(salt);
        byte[] dst = new byte[src.Length + bytes.Length];
        Buffer.BlockCopy(src, 0, dst, 0, src.Length);
        Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
        HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
        byte[] inArray = algorithm.ComputeHash(dst);
        return Convert.ToBase64String(inArray);
    }

    static void Main()
    {
        string pass = "test123";
        string salt = "/Eju9rmaJp03e3+z1v5s+A==";
        string hash = Program.EncodePassword(pass,salt);
        Console.WriteLine(hash);
        // outputs jj/rf7OxXM263rPgvLan4M6Is7o=
    }
}

所以现在只需要将这个算法移植到 node.js 中即可。问题是 c# 以某种方式神奇地对字节进行操作,我不知道如何在 Node 中进行操作。考虑以下代码(它不使用任何盐 - 它只是从密码创建 base64 sha1:

crypto = require("crypto")
pass = 'test123'
sha1 = crypto.createHash("sha1")
buf = new Buffer( pass, 'utf8')
sha1.update(buf)
result = sha1.digest("base64")
console.log(result)
// outputs cojt0Pw//L6ToM8G41aOKFIWh7w=

在 C# 中

 using System.Text;
 using System.Security.Cryptography;
 string pass = "test123";
 byte[] bytes = Encoding.Unicode.GetBytes(pass);
 HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
 byte[] inArray = algorithm.ComputeHash(bytes);
 string hash = Convert.ToBase64String(inArray);
 Console.WriteLine(hash);
 // outputs Oc/baVMs/zM28IqDqsQlJPQc1uk=

我需要 node.js 中的代码,它将返回与 c# 中的代码相同的值。有什么想法吗?

最佳答案

我终于在这里找到了正确答案:https://gist.github.com/PalmerEk/1191651 (从“ucs2”到“utf16le”几乎没有变化):

function dotnet_membership_password_hash(pass, salt)
{
  var bytes = new Buffer(pass || '', 'utf16le');
  var src = new Buffer(salt || '', 'base64');
  var dst = new Buffer(src.length + bytes.length);
  src.copy(dst, 0, 0, src.length);
  bytes.copy(dst, src.length, 0, bytes.length);

  return crypto.createHash('sha1').update(dst).digest('base64');
}

关于asp.net - 如何在 node.js 中检查 ASP.NET 密码哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25745974/

相关文章:

css - 对齐复选框列表中的复选框

node.js - npm 启动时 webpack-dev-server 错误

node.js - Node ImageMagick 在本地成功,但在 AWS Lambda 中失败

algorithm - 铁路轨道的排列(堆栈实现)

algorithm - 这不就是解决0-1背包问题的一种正确但非常高效简单的方法吗?

c# - C# 中的 SOAP 序列化忽略对象但包含其数据成员

c# - ASP.NET Health Monitoring or ELMAH 选什么?

asp.net - 在必填字段验证器中进行数字验证

javascript - 在 Node.js 中使用 jsonwebtokens 进行身份验证

python - 显示分水岭算法的分割