feat: encryption
This commit is contained in:
@@ -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) => {
|
||||||
@@ -19,4 +20,12 @@ export const hashPassword = (password: string): Promise<string> => {
|
|||||||
// password from frontend and hash from database
|
// password from frontend and hash from database
|
||||||
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}`;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user