From fef0464f40fd96fcde2c48b732bbab245d8e15a7 Mon Sep 17 00:00:00 2001 From: kv272 Date: Sun, 28 Jul 2024 19:42:12 +0530 Subject: [PATCH] revised version of the code for better readability --- Nodejs API/utils/password-hash.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Nodejs API/utils/password-hash.js b/Nodejs API/utils/password-hash.js index 0680a20..b5bc450 100644 --- a/Nodejs API/utils/password-hash.js +++ b/Nodejs API/utils/password-hash.js @@ -1,19 +1,27 @@ -/* -* Real time private chatting app using Angular 2, Nodejs, mongodb and Socket.io -* @author Shashank Tiwari -*/ 'use strict'; const bcrypt = require('bcrypt'); -class PasswordHash{ +class PasswordHash { + constructor() { + this.saltRounds = 10; // Number of salt rounds for hashing + } - createHash(password) { - return bcrypt.hashSync(password, 10); - } + async createHash(password) { + try { + const salt = await bcrypt.genSalt(this.saltRounds); + return await bcrypt.hash(password, salt); + } catch (error) { + throw new Error('Error creating hash'); + } + } - compareHash(password, hash) { - return bcrypt.compareSync(password, hash) - } + async compareHash(password, hash) { + try { + return await bcrypt.compare(password, hash); + } catch (error) { + throw new Error('Error comparing hash'); + } + } } module.exports = new PasswordHash();