Skip to content

Instantly share code, notes, and snippets.

@robert-kratz
Created January 18, 2022 19:41
Show Gist options
  • Save robert-kratz/4fbfa83753c1be26386187c1d16e1f0c to your computer and use it in GitHub Desktop.
Save robert-kratz/4fbfa83753c1be26386187c1d16e1f0c to your computer and use it in GitHub Desktop.
NodeJS Bycript hashing methods
const bcrypt = require('bcrypt');
module.exports = {
/**
* Use this to hash a password
* @param {String} input
* @returns Promise<String>
*/
encript: (input) => {
return new Promise((resolve, rejects) => {
bcrypt.hash(input, 10, (err, hash) => {
if(err) return rejects(err);
return resolve(hash);
});
});
},
/**
* Use this method to check if the hash is equals the plain password
* @param {String} plain
* @param {String} hash
* @returns Promise<Boolean>
*/
check: (plain, hash) => {
return new Promise((resolve, rejects) => {
bcrypt.compare(plain, hash, (err, result) => {
if(err) return rejects(err);
return resolve(result);
})
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment