( password: string, hash: string, keyLength = 32, )
| 36 | * @returns {boolean} |
| 37 | */ |
| 38 | export async function verifyPassword( |
| 39 | password: string, |
| 40 | hash: string, |
| 41 | keyLength = 32, |
| 42 | ): Promise<boolean> { |
| 43 | return new Promise((resolve, reject) => { |
| 44 | const [salt, hashKey] = hash.split('.'); |
| 45 | // we need to pass buffer values to timingSafeEqual |
| 46 | const hashKeyBuff = Buffer.from(hashKey!, 'hex'); |
| 47 | scrypt(password, salt!, keyLength, (err, derivedKey) => { |
| 48 | if (err) { |
| 49 | reject(err); |
| 50 | } |
| 51 | // compare the new supplied password with the hashed password using timeSafeEqual |
| 52 | resolve( |
| 53 | timingSafeEqual( |
| 54 | new Uint8Array(hashKeyBuff), |
| 55 | new Uint8Array(derivedKey), |
| 56 | ), |
| 57 | ); |
| 58 | }); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | export function createHash(data: string, len: number) { |
| 63 | return cryptoCreateHash('shake256', { outputLength: len }) |
no test coverage detected