chore:little fixes and formating and linting and patches
This commit is contained in:
@@ -16,13 +16,15 @@ export function generateSalt() {
|
||||
*/
|
||||
export async function hashPassword(
|
||||
password: string,
|
||||
keyLength = 32,
|
||||
keyLength = 32
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// generate random 16 bytes long salt - recommended by NodeJS Docs
|
||||
const salt = generateSalt();
|
||||
scrypt(password, salt, keyLength, (err, derivedKey) => {
|
||||
if (err) reject(err);
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
// derivedKey is of type Buffer
|
||||
resolve(`${salt}.${derivedKey.toString('hex')}`);
|
||||
});
|
||||
@@ -38,7 +40,7 @@ export async function hashPassword(
|
||||
export async function verifyPassword(
|
||||
password: string,
|
||||
hash: string,
|
||||
keyLength = 32,
|
||||
keyLength = 32
|
||||
): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const [salt, hashKey] = hash.split('.');
|
||||
@@ -50,10 +52,7 @@ export async function verifyPassword(
|
||||
}
|
||||
// compare the new supplied password with the hashed password using timeSafeEqual
|
||||
resolve(
|
||||
timingSafeEqual(
|
||||
new Uint8Array(hashKeyBuff),
|
||||
new Uint8Array(derivedKey),
|
||||
),
|
||||
timingSafeEqual(new Uint8Array(hashKeyBuff), new Uint8Array(derivedKey))
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user