Development Tip

node.bcrypt.js는 솔트가없는 해시 된 암호와 일반 텍스트 암호를 어떻게 비교합니까?

yourdevel 2020. 10. 12. 08:14
반응형

node.bcrypt.js는 솔트가없는 해시 된 암호와 일반 텍스트 암호를 어떻게 비교합니까?


에서 GitHub의 :

암호를 해시하려면 :

var bcrypt = require('bcrypt');
bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash("B4c0/\/", salt, function(err, hash) {
        // Store hash in your password DB.
    });
});

비밀번호를 확인하려면 :

// Load hash from your password DB.
bcrypt.compare("B4c0/\/", hash, function(err, res) {
    // res == true
});
bcrypt.compare("not_bacon", hash, function(err, res) {
    // res = false
});

위에서 어떻게 비교에 소금 값이 없을 수 있습니까? 내가 여기서 무엇을 놓치고 있습니까?


솔트는 해시에 통합됩니다 (일반 텍스트). 비교 함수는 단순히 해시에서 솔트를 가져온 다음이를 사용하여 암호를 해시하고 비교를 수행합니다.


나도 원래 포스터와 같은 질문을했고 메커니즘을 이해하기 위해 주위를 둘러 보며 여러 가지 시도를했습니다. 다른 사람들이 이미 지적했듯이 솔트는 최종 해시에 연결됩니다. 따라서 이것은 몇 가지를 의미합니다.

  1. 알고리즘은 소금의 길이를 알아야합니다.
  2. 최종 문자열에서 소금의 위치도 알아야합니다. 예를 들어 왼쪽 또는 오른쪽에서 특정 숫자만큼 오프셋 된 경우.

이 두 가지가 일반적으로 하드 구현에 코딩 등의 bcrypt 구현 소스 bcryptjs는 16 소금 길이를 정의

/**
* @type {number}
* @const
* @private
*/

var BCRYPT_SALT_LEN = 16;

따라서 아이디어의 기본 개념을 설명하기 위해 수동으로 수행하려는 경우 다음과 유사하게 보일 것입니다. 나는 당신이 그것을 할 수있는 라이브러리가있을 때 이와 같은 것을 직접 구현하는 것을 권장하지 않습니다.

var salt_length = 16;
var salt_offset = 0;

var genSalt = function(callback)
{
    var alphaNum = '0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ';
    var salt = '';
    for (var i = 0; i < salt_length; i++) {
        var j = Math.floor(Math.random() * alphaNum.length);
        salt += alphaNum[j];
    }
    callback(salt);
}

// cryptographic hash function of your choice e.g. shar2
// preferably included from an External Library (dont reinvent the wheel)
var shar2 = function(str) {
    // shar2 logic here 
    // return hashed string;
}

var hash = function(passwordText, callback)
{
    var passwordHash = null;
    genSalt(function(salt){
        passwordHash = salt + shar2(passwordText + salt);
    });

    callback(null, passwordHash);
}

var compare = function(passwordText, passwordHash, callback)
{
    var salt = passwordHash.substr(salt_offset, salt_length);
    validatedHash = salt + shar2(passwordText + salt);

    callback(passwordHash === validatedHash);   
}

// sample usage
var encryptPassword = function(user)
{
    // user is an object with fields like username, pass, email
    hash(user.pass, function(err, passwordHash){
        // use the hashed password here
        user.pass = passwordHash;
    });

    return user;
}

var checkPassword = function(passwordText, user)
{
    // user has been returned from database with a hashed password
    compare(passwordText, user.pass, function(result){
        // result will be true if the two are equal
        if (result){
            // succeeded
            console.log('Correct Password');
        }
        else {
            // failed
            console.log('Incorrect Password');
        }
    });
}

참고 URL : https://stackoverflow.com/questions/13023361/how-does-node-bcrypt-js-compare-hashed-and-plaintext-passwords-without-the-salt

반응형