node.js 中的 Django 密码

标签 django node.js bcrypt

我正在尝试从我之前在 node.js 中的 django web 应用程序进行一些身份验证。我让 PBKDF2-sha256 工作,但我无法让 BCryptSHA256PasswordHasher 在 Node 中工作。我尝试了以下方法:

var Bcrypt = require('bcrypt');
var sha256 = require('sha256');

var pass = sha256("test password")

// from django ("bcrypt_sha256$$2b$12$mUg9hoKn0tt2/VwWaNb6Euie4.jtQjfU6.CY1pT0EH8GPORqAsh66")
var hash = "$2b$12$mUg9hoKn0tt2/VwWaNb6Euie4.jtQjfU6.CY1pT0EH8GPORqAsh66" 
Bcrypt.compare(pass, hash, function (err, isMatch) {
    if (err) {
        return console.error(err);
    }
    console.log('do they match?', isMatch);
});

上面有什么我遗漏的吗?我正在获取密码的 sha256 并使用 bcrypt 进行测试。 Django中对应的代码如下:

def verify(self, password, encoded):
    algorithm, data = encoded.split('$', 1)
    assert algorithm == self.algorithm
    bcrypt = self._load_library()

    # Hash the password prior to using bcrypt to prevent password truncation
    #   See: https://code.djangoproject.com/ticket/20138
    if self.digest is not None:
        # We use binascii.hexlify here because Python3 decided that a hex encoded
        #   bytestring is somehow a unicode.
        password = binascii.hexlify(self.digest(force_bytes(password)).digest())
    else:
        password = force_bytes(password)

    # Ensure that our data is a bytestring
    data = force_bytes(data)
    # force_bytes() necessary for py-bcrypt compatibility
    hashpw = force_bytes(bcrypt.hashpw(password, data))

    return constant_time_compare(data, hashpw)

更新

我不知道为什么,但是当我将盐稍微更改为以下内容时:

var hash = "$2a$12$mUg9hoKn0tt2/VwWaNb6Euie4.jtQjfU6.CY1pT0EH8GPORqAsh66" 

一切正常!我把开头的2b改成了2a。为什么这个有效而另一个无效?有什么我想念的吗?

最佳答案

来自excellent Passlib library :

  1. ident (str) – Specifies which version of the BCrypt algorithm will be used when creating a new hash. Typically this option is not needed, as the default ("2a") is usually the correct choice. If specified, it must be one of the following:
    • "2" - the first revision of BCrypt, which suffers from a minor security flaw and is generally not used anymore. "2a" - some implementations suffered from a very rare security flaw. current default for compatibility purposes.
    • "2y" - format specific to the crypt_blowfish BCrypt implementation, identical to "2a" in all but name.
    • "2b" - latest revision of the official BCrypt algorithm (will be default in Passlib 1.7).

关于node.js 中的 Django 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487430/

相关文章:

python - Django 手动渲染表单并使用 csrf token

django - 如何使用 Django-Python 创建 "beautiful"URL

javascript - 如何跨文件编写单例 Node 模块?

java - 比较两个相同来源的 BCRYPTed 密码

ruby-on-rails - 如何更改 Rails 4 has_secure_password 状态消息?

Django - 发送有关模型更改的电子邮件

python - Django 多对多字段

javascript - 此 JS 代码查找数组中最大值的索引,但它是如何工作的?

node.js - 基于环回 redis 的 session

javascript - MEAN 应用程序错误预期对象