Skip to content

Instantly share code, notes, and snippets.

@gameshler
Created July 15, 2025 17:38
Show Gist options
  • Select an option

  • Save gameshler/9f3607e31c80ce020eba8c9d1973426e to your computer and use it in GitHub Desktop.

Select an option

Save gameshler/9f3607e31c80ce020eba8c9d1973426e to your computer and use it in GitHub Desktop.
Reusable functions to hash and compare sensitive values using bcryptjs in TypeScript. Defaults to 12 salt rounds. Gracefully handles comparison errors. Ideal for a secure user authentication.
import bcrypt from "bcryptjs";
export const hashValue = async (value: string, saltRounds?: number) =>
bcrypt.hash(value, saltRounds || 12);
export const compareValue = async (value: string, hashedValue: string) =>
bcrypt.compare(value, hashedValue).catch(() => false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment