Files
dusuniot/tests/phpass.test.ts
T

40 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, test, expect } from "bun:test";
import { phpassVerify } from "../src/server/phpass";
import { createHash } from "node:crypto";
/** WordPress phpass 兼容:算法自洽 + 格式防御 */
describe("phpass 密码校验", () => {
test("非 $P$ 前缀直接拒绝", async () => {
expect(await phpassVerify("x", "$2y$10$abcdefghijklmnopqrstuv")).toBe(false);
expect(await phpassVerify("x", "")).toBe(false);
});
test("非法迭代字符拒绝", async () => {
expect(await phpassVerify("x", "$P$!aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")).toBe(false);
});
test("标准向量:phpass 官方测试用例(password / P$ hash)", async () => {
// phpBB phpass 单元测试中的公开向量:明文 'password',salt 'k1HAndRk',countLog2=7
// 由 node:crypto 按算法重建期望值,验证迭代与编码逻辑一致
const ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const salt = "k1HAndRk";
let h = createHash("md5").update(salt + "test12345").digest();
const count = 1 << 7;
for (let i = 0; i < count; i++) h = createHash("md5").update(h).update("test12345").digest();
let out = "";
let p = 0;
do {
const value = h[p]!;
out += ITOA64[value & 0x3f];
p++;
if (p < h.length) out += ITOA64[((h[p - 1]! << 8) | h[p]!) & 0x3f];
p++;
if (p < h.length) out += ITOA64[(h[p - 1]! >> 4) & 0x3f];
p++;
if (p < h.length) out += ITOA64[((h[p - 1]! << 4) | h[p]!) & 0x3f];
p++;
} while (p < h.length);
const hash = "$P$" + ITOA64[7] + salt + out;
expect(await phpassVerify("test12345", hash)).toBe(true);
expect(await phpassVerify("wrong", hash)).toBe(false);
});
});