Last active
January 25, 2025 09:46
-
-
Save fsjorgeluis/3e7818ec3b4b5c3d2619c5a293174c36 to your computer and use it in GitHub Desktop.
Functions to hash and compare password using bcrypt library and typescript
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 * as bcrypt from 'bcrypt'; | |
/** | |
* Hash password using bcrypt library | |
* @param password String | |
* @returns string type | |
*/ | |
export const HashPassword = async (password: string): Promise<string> => { | |
const salt = await bcrypt.genSalt(); | |
const hash = await bcrypt.hash(password, salt); | |
return hash; | |
} | |
/** | |
* Compare hashed password using bcrypt library | |
* @param hash String | |
* @param password String | |
* @returns boolean type | |
*/ | |
export const ComparePassword = async (hash: string, password: string): Promise<boolean> => { | |
const isMatch = await bcrypt.compare(password, hash); | |
return isMatch; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You’re welcome :)