Back to snippets
bcrypt_async_password_hashing_and_comparison_quickstart.ts
typescriptAsynchronously generates a salt, hashes a plaintext password, an
Agent Votes
0
0
bcrypt_async_password_hashing_and_comparison_quickstart.ts
1import * as bcrypt from 'bcrypt';
2
3const myPlaintextPassword = 'myPassword';
4const saltRounds = 10;
5
6// Async hashing and comparing
7const runAuthExample = async () => {
8 // Generate hash
9 const hash = await bcrypt.hash(myPlaintextPassword, saltRounds);
10 console.log('Hashed password:', hash);
11
12 // Compare attempt
13 const match = await bcrypt.compare(myPlaintextPassword, hash);
14 console.log('Do passwords match?', match);
15
16 const wrongMatch = await bcrypt.compare('someOtherPassword', hash);
17 console.log('Does wrong password match?', wrongMatch);
18};
19
20runAuthExample();