( password: string, keyLength = 32, )
| 15 | * @returns {string} The salt+hash |
| 16 | */ |
| 17 | export async function hashPassword( |
| 18 | password: string, |
| 19 | keyLength = 32, |
| 20 | ): Promise<string> { |
| 21 | return new Promise((resolve, reject) => { |
| 22 | // generate random 16 bytes long salt - recommended by NodeJS Docs |
| 23 | const salt = generateSalt(); |
| 24 | scrypt(password, salt, keyLength, (err, derivedKey) => { |
| 25 | if (err) reject(err); |
| 26 | // derivedKey is of type Buffer |
| 27 | resolve(`${salt}.${derivedKey.toString('hex')}`); |
| 28 | }); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Compare a plain text password with a salt+hash password |
no test coverage detected