JavaScriptとbcryptでdbに保存する前にパスワードをハッシュする方法.
2790 ワード
ユーザー名、電子メール、電話番号、およびその他のデータのようなユーザーからデータを保存するときは、通常、これらのデータをプレーンテキストで保存しますが、ユーザーパスワードに対しては安全ではありません.
だから、DBに保存する前にすべてのパスワードをハッシュ、通常は良い練習です.bcryptは、この提案のためのJavaScriptライブラリです.
NPM INITを使ってinit npmプロジェクトを覚えておいてください.
今すぐあなたのライブラリを実装します.
ありがとう
だから、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.
}
});
それはすべてです.ありがとう
Reference
この問題について(JavaScriptとbcryptでdbに保存する前にパスワードをハッシュする方法.), 我々は、より多くの情報をここで見つけました https://dev.to/calguzman/how-to-encrypt-a-password-before-save-into-db-with-javascript-and-bcrypt-1p7bテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol