Files
2025-01-04 00:34:03 +01:00

1 line
629 B
JavaScript

import crypto from"node:crypto";const _S3KT="17c4f39053ac5a50d5797c665ad1f4e6";export function createPassword(t,r){return{hash:hashPassword(t,r=r||randomString(64)),salt:r}}export function getSalt(t){return"string"==typeof(t=t??globalThis.options?.passwordSalt)&&t.length?t:_S3KT}export function randomString(t=16){return crypto.randomBytes(t).toString("hex").slice(0,t)}export function hashPassword(t,r){return crypto.pbkdf2Sync(t,r,1e3,64,"sha512").toString("hex")}export function testPassword(t,r,o){const n=crypto.pbkdf2Sync(t,o,1e3,64,"sha512"),e=Buffer.from(r,"hex");return n.length===e.length&&crypto.timingSafeEqual(n,e)}