feat: use getEnvVariable to encrypt & decrypt

This commit is contained in:
karishmas6
2024-10-05 15:55:20 +05:30
parent 09b6c9c265
commit 09647510c5

View File

@@ -1,5 +1,6 @@
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import crypto from 'crypto'; import crypto from 'crypto';
import { getEnvVariable } from './env';
export const hashPassword = (password: string): Promise<string> => { export const hashPassword = (password: string): Promise<string> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -23,8 +24,11 @@ export const comparePassword = (password: string, hash: string): Promise<boolean
} }
const encrypt = (text: string): string => { const encrypt = (text: string): string => {
const iv = crypto.randomBytes(process.env.IV_LENGTH); const ivLength = parseInt(getEnvVariable('IV_LENGTH'), 10);
const cipher = crypto.createCipheriv(process.env.ALGORITHM, Buffer.from(process.env.ENCRYPTION_KEY), iv); const iv = crypto.randomBytes(ivLength);
const algorithm = getEnvVariable('ALGORITHM');
const key = Buffer.from(getEnvVariable('ENCRYPTION_KEY'), 'hex');
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex'); let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex'); encrypted += cipher.final('hex');
return `${iv.toString('hex')}:${encrypted}`; return `${iv.toString('hex')}:${encrypted}`;
@@ -32,7 +36,9 @@ const encrypt = (text: string): string => {
const decrypt = (encryptedText: string): string => { const decrypt = (encryptedText: string): string => {
const [iv, encrypted] = encryptedText.split(':'); const [iv, encrypted] = encryptedText.split(':');
const decipher = crypto.createDecipheriv(process.env.ALGORITHM, Buffer.from(process.env.ENCRYPTION_KEY), Buffer.from(iv, 'hex')); const algorithm = getEnvVariable('ALGORITHM');
const key = Buffer.from(getEnvVariable('ENCRYPTION_KEY'), 'hex');
const decipher = crypto.createDecipheriv(algorithm, key, Buffer.from(iv, 'hex'));
let decrypted = decipher.update(encrypted, 'hex', 'utf8'); let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8'); decrypted += decipher.final('utf8');
return decrypted; return decrypted;