Skip to content

Instantly share code, notes, and snippets.

@fsjorgeluis
Last active January 25, 2025 09:46
Show Gist options
  • Save fsjorgeluis/3e7818ec3b4b5c3d2619c5a293174c36 to your computer and use it in GitHub Desktop.
Save fsjorgeluis/3e7818ec3b4b5c3d2619c5a293174c36 to your computer and use it in GitHub Desktop.
Functions to hash and compare password using bcrypt library and typescript
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;
}
@fsjorgeluis
Copy link
Author

Merci beaucoup.

You’re welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment