@dyrected/core 2.5.11 → 2.5.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ // src/auth/password.ts
2
+ import { promisify } from "util";
3
+ import { scrypt, randomBytes, timingSafeEqual } from "crypto";
4
+ var scryptAsync = promisify(scrypt);
5
+ var SALT_LEN = 16;
6
+ var KEY_LEN = 64;
7
+ async function hashPassword(plain) {
8
+ const salt = randomBytes(SALT_LEN).toString("hex");
9
+ const derivedKey = await scryptAsync(plain, salt, KEY_LEN);
10
+ return `${salt}:${derivedKey.toString("hex")}`;
11
+ }
12
+ async function verifyPassword(plain, stored) {
13
+ const [salt, storedHash] = stored.split(":");
14
+ if (!salt || !storedHash) return false;
15
+ const derivedKey = await scryptAsync(plain, salt, KEY_LEN);
16
+ const storedBuffer = Buffer.from(storedHash, "hex");
17
+ if (derivedKey.length !== storedBuffer.length) return false;
18
+ return timingSafeEqual(derivedKey, storedBuffer);
19
+ }
20
+
21
+ export {
22
+ hashPassword,
23
+ verifyPassword
24
+ };