-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deps: switch from bcrypt to bcryptjs (#4640)
* test: add test for BCryptHasher * chore(deps): replace `bcrypt` with `bcryptjs` * enhance: simplify BCryptHasher
- Loading branch information
1 parent
3ad6c45
commit 38bd274
Showing
4 changed files
with
67 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, expect, it } from "vitest" | ||
import { BCryptHasher } from "./hashers.js" | ||
|
||
describe(BCryptHasher, () => { | ||
it("should encode a password", async () => { | ||
const hasher = new BCryptHasher() | ||
hasher.saltRounds = 4 // for faster tests | ||
const password = "password" | ||
const hash = await hasher.encode(password) | ||
expect(hash).toContain("bcrypt") | ||
}) | ||
|
||
it("should verify a hashed password", async () => { | ||
const hasher = new BCryptHasher() | ||
hasher.saltRounds = 4 // for faster tests | ||
const password = "password" | ||
const hash = await hasher.encode(password) | ||
const result = await hasher.verify(password, hash) | ||
expect(result).toBe(true) | ||
}) | ||
|
||
it("should verify a pre-hashed password", async () => { | ||
const hasher = new BCryptHasher() | ||
const password = "password" | ||
const hashedPasswords = | ||
// hashes for "password" | ||
[ | ||
"bcrypt$$2b$12$TF65ro5CW6A5Ai2qvVOSsO9h/rZbYMI19kX2CLV/7F5VeeHZkTJaC", | ||
"bcrypt$$2b$04$JlvbPo81NHviVoeMv1DVTu0QmhB9K21jnaaYgMC.qShgQ0uyhfR.S", | ||
] | ||
|
||
for (const hashedPassword of hashedPasswords) { | ||
const result = await hasher.verify(password, hashedPassword) | ||
expect(result).toBe(true) | ||
} | ||
}) | ||
|
||
it("should not verify a wrong password", async () => { | ||
const hasher = new BCryptHasher() | ||
const password = "wrongPassword" | ||
const hashedPassword = | ||
"bcrypt$$2b$12$TF65ro5CW6A5Ai2qvVOSsO9h/rZbYMI19kX2CLV/7F5VeeHZkTJaC" // hash for "password" | ||
const result = await hasher.verify(password, hashedPassword) | ||
expect(result).toBe(false) | ||
}) | ||
}) |
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