JavaScriptとbcryptでdbに保存する前にパスワードをハッシュする方法.

2790 ワード

ユーザー名、電子メール、電話番号、およびその他のデータのようなユーザーからデータを保存するときは、通常、これらのデータをプレーンテキストで保存しますが、ユーザーパスワードに対しては安全ではありません.
だから、DBに保存する前にすべてのパスワードをハッシュ、通常は良い練習です.bcryptは、この提案のためのJavaScriptライブラリです.
NPM INITを使ってinit npmプロジェクトを覚えておいてください.
今すぐあなたのライブラリを実装します.
const bcrypt = require('bcrypt'); // import the Library. 
const saltRounds = 10; // The number of rounds for encrypt the passwords. 
const myPlaintextPassword = 'examplePassword';

// Now, use bcrypt for encrypt the plain Password. 

bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
    // Store hash in your password DB.
    console.log(hash); 
});

あなたが保存されたパスワードで非プレーンのパスワードを比較する必要がある場合は、関数の比較を使用することができます.
bcrypt.compare(myPlaintextPassword, hash, function(err, result) {
    // result == true
if(result==true){
 // The Password is Correct!
}
else {
 // Your password is not correct. 
}
});

それはすべてです.
ありがとう