-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use bcrypt in social network example (#371)
- Loading branch information
1 parent
bc7eba0
commit c823ce4
Showing
5 changed files
with
12 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
import { randomBytes, createCipher, createDecipher } from 'crypto'; | ||
import { hash, compare } from 'bcrypt-as-promised'; | ||
|
||
export function generateSalt() { | ||
return randomBytes(16).toString('hex'); | ||
} | ||
|
||
export function encryptPassword(str, secret) { | ||
let encrypted; | ||
const cipher = createCipher('aes-256-ctr', secret); | ||
const saltRounds = 10; | ||
|
||
encrypted = cipher.update(str, 'utf8', 'hex'); | ||
encrypted += cipher.final('hex'); | ||
return encrypted; | ||
export function hashPassword(password) { | ||
return hash(password, saltRounds); | ||
} | ||
|
||
export function decryptPassword(hash, secret) { | ||
let decrypted; | ||
const decipher = createDecipher('aes-256-ctr', secret); | ||
|
||
decrypted = decipher.update(hash, 'hex', 'utf8'); | ||
decrypted += decipher.final('utf8'); | ||
return decrypted; | ||
export function comparePassword(password, hash) { | ||
return compare(password, hash) | ||
} |
This file contains 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
This file contains 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