import bcrypt from "bcrypt"; import crypto from 'crypto'; export const hashPassword = (password: string): Promise => { return new Promise((resolve, reject) => { bcrypt.genSalt(12, (err, salt) => { if (err) { reject(err) } bcrypt.hash(password, salt, (err, hash) => { if (err) { reject(err) } resolve(hash) }) }) }) } // password from frontend and hash from database export const comparePassword = (password: string, hash: string): Promise => { return bcrypt.compare(password, hash) } const encrypt = (text: string): string => { const iv = crypto.randomBytes(IV_LENGTH); const cipher = crypto.createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY), iv); let encrypted = cipher.update(text, 'utf8', 'hex'); encrypted += cipher.final('hex'); return `${iv.toString('hex')}:${encrypted}`; };