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 bcrypt from "bcrypt";
import crypto from 'crypto';
export const hashPassword = (password: string): Promise<string> => { export const hashPassword = (password: string): Promise<string> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -20,3 +21,11 @@ export const hashPassword = (password: string): Promise<string> => {
export const comparePassword = (password: string, hash: string): Promise<boolean> => { export const comparePassword = (password: string, hash: string): Promise<boolean> => {
return bcrypt.compare(password, hash) 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}`;
};