Skip to content

Commit

Permalink
Fix LegacyScrypt (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Jan 28, 2024
1 parent 44b028b commit 075eb8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .auri/$e3mj84c4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "lucia" # package name
type: "patch" # "major", "minor", "patch"
---

Fix `LegacyScrypt` generating malformed hash (see PR for fix)
11 changes: 10 additions & 1 deletion packages/lucia/src/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { Scrypt } from "./crypto.js";
import { Scrypt, LegacyScrypt } from "./crypto.js";
import { encodeHex } from "oslo/encoding";

test("validateScryptHash() validates hashes generated with generateScryptHash()", async () => {
Expand All @@ -10,3 +10,12 @@ test("validateScryptHash() validates hashes generated with generateScryptHash()"
const falsePassword = encodeHex(crypto.getRandomValues(new Uint8Array(32)));
await expect(scrypt.verify(hash, falsePassword)).resolves.toBe(false);
});

test("LegacyScrypt", async () => {
const password = encodeHex(crypto.getRandomValues(new Uint8Array(32)));
const scrypt = new LegacyScrypt();
const hash = await scrypt.hash(password);
await expect(scrypt.verify(hash, password)).resolves.toBe(true);
const falsePassword = encodeHex(crypto.getRandomValues(new Uint8Array(32)));
await expect(scrypt.verify(hash, falsePassword)).resolves.toBe(false);
})
2 changes: 1 addition & 1 deletion packages/lucia/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class LegacyScrypt implements PasswordHashingAlgorithm {
async hash(password: string): Promise<string> {
const salt = encodeHex(crypto.getRandomValues(new Uint8Array(16)));
const key = await generateScryptKey(password.normalize("NFKC"), salt);
return `${salt}:${encodeHex(key)}`;
return `s2:${salt}:${encodeHex(key)}`;
}
async verify(hash: string, password: string): Promise<boolean> {
const parts = hash.split(":");
Expand Down

0 comments on commit 075eb8b

Please sign in to comment.