feat: encryption

This commit is contained in:
karishmas6
2024-10-05 15:42:06 +05:30
parent a68955e3b4
commit 573541e21a

View File

@@ -1,4 +1,5 @@
import bcrypt from "bcrypt";
import crypto from 'crypto';
export const hashPassword = (password: string): Promise<string> => {
return new Promise((resolve, reject) => {
@@ -19,4 +20,12 @@ export const hashPassword = (password: string): Promise<string> => {
// password from frontend and hash from database
export const comparePassword = (password: string, hash: string): Promise<boolean> => {
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}`;
};