Created
January 16, 2019 12:42
-
-
Save Sammuel09/4bb6cd0d19a1047bdb43c42d9b66ad65 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import bcrypt from 'bcrypt'; | |
/** | |
* @class passwordManager | |
* @description class defines the methods used to hash and compare hashed passswords. | |
*/ | |
class PasswordManager { | |
/** | |
* @static | |
* @param {string} password to be hashed | |
* @returns {string} - hashed password. | |
* @memberof encryptData | |
*/ | |
static async hashPassword(password) { | |
const salt = 10; | |
try { | |
const hashedPassword = await bcrypt.hashSync(password, salt); | |
return hashedPassword; | |
} catch (err) { | |
return Error('An error occured while hashing'); | |
} | |
} | |
/** | |
* @static | |
* @param {string} password - the string to compare | |
* @param {string} hashedpassword - hashed password from the database | |
* that password is compared against. | |
* @returns {boolean} - this is the result of the comparism | |
* If the password and the hashed password in the database are the same | |
* it returns true, else it returns false. | |
* @memberof encryptData | |
*/ | |
static async decryptPassword(password, hashedpassword) { | |
try { | |
const isPasswordValid = await bcrypt.compareSync(password, hashedpassword); | |
return isPasswordValid; | |
} catch (err) { | |
return Error('An error occured while comparing the hashed password.'); | |
} | |
} | |
} | |
export default PasswordManager; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment