2

私の同僚は、アカウント情報を保存するデータベースを持っています。アカウントの SHA256 ハッシュ化されたパスワードとソルト値は、生のバイナリ データ (ブロブ) として列に保存されます。

パスワードは、これを使用して PHP でハッシュされます (true は生の出力を示します)。

hash("sha256", $salt . $password, true);

PHPからデータベースに保存されているのと同じハッシュ化されたパスワードを取得する必要があるNode.jsサーバーに認証を実装しようとしていますが、これは機能していないようです:

/**
 * Validates a password sent by an end user by comparing it to the 
 * hashed password stored in the database. Uses the Node.js crypto library.
 *
 * @param password The password sent by the end user.
 * @param dbPassword The hashed password stored in the database.
 * @param dbSalt The encryption salt stored in the database.
 */
function validatePassword(password, dbPassword, dbSalt) {
    // Should the dbSalt be a Buffer, hex, base64, or what?
    var hmac = crypto.createHmac("SHA256", dbSalt);
    var hashed = hmac.update(password).digest('base64');
    console.log("Hashed user password: " + hashed);
    console.log("Database password: " + dbPassword.toString('base64'));
    return hashed === dbPassword;
}
4

2 に答える 2

5

多くの実験で、私は解決策を見つけました。

/**
 * Encrypts a password using sha256 and a salt value.
 *
 * @param password The password to hash.
 * @param salt The salt value to hash with.
 */
function SHA256Encrypt(password, salt) {
    var saltedpassword = salt + password;
    var sha256 = crypto.createHash('sha256');
    sha256.update(saltedpassword);
    return sha256.digest('base64');
}

/**
 * Validates a password sent by an end user by comparing it to the
 * hashed password stored in the database.
 *
 * @param password The password sent by the end user.
 * @param dbPassword The hashed password stored in the database, encoded in Base64.
 * @param dbSalt The encryption salt stored in the database. This should be a raw blob.
 */
function validatePassword(password, dbPassword, dbSalt) {
    var hashed = SHA256Encrypt(password, dbSalt.toString('binary'));
    return hashed === dbPassword;
}

しかし、TravisO のおかげで、彼は私を正しい道に導いてくれました。

于 2013-06-20T20:09:05.877 に答える
2

crypto.createHash()

http://nodejs.org/docs/v0.6.18/api/crypto.html#crypto_crypto_createhash_algorithm

まったく同じハッシュ タイプとソルトを使用していることを確認してください。

于 2013-06-20T18:25:17.703 に答える