Created
January 18, 2022 19:41
-
-
Save robert-kratz/4fbfa83753c1be26386187c1d16e1f0c to your computer and use it in GitHub Desktop.
NodeJS Bycript hashing methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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