Django password in node.js

I am trying to do some authentication from my previous django web application in node. I got it PBKDF2-sha256, but I can't get it BCryptSHA256PasswordHasherworking in node. I tried the following:

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);
});

Is there something I am missing with the above? I take the sha256password and test with bcrypt. The corresponding code in Django is below:

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)

UPDATE

I have no idea why, but when I slightly change the salt to the following:

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

everything is working! I switched 2bto 2aat the beginning. Why does this work, but not the other? Is there something I'm missing?

+4
source share
1 answer

Passlib:

  • ident (str) - , BCrypt . , ( "2a" ) . , :
    • "2" - BCrypt, , , . "2a" - . .
    • "2y" - , BCrypt crypt_blowfish, "2a" , .
    • "2b" - BCrypt ( Passlib 1.7).
0
source

Source: https://habr.com/ru/post/1629410/


All Articles