-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update encryption logic for SMTP password
This commit refactors the code to update the encryption logic for the SMTP password in the `User` model. The `encrypt` function from the newly added `crypt.ts` file is now used to encrypt the password before storing it.
- Loading branch information
Showing
4 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
import { generateKeySync } from "crypto"; | ||
import { randomBytes } from "crypto"; | ||
|
||
export const PYLON_URL = process.env.PYLON_URL || "http://localhost:3000"; | ||
export const PYLON_SECRET = | ||
process.env.PYLON_SECRET || | ||
generateKeySync("hmac", { | ||
length: 32, | ||
}) | ||
.export() | ||
.toString("base64"); | ||
process.env.PYLON_SECRET || randomBytes(32).toString("hex"); // 32 bytes for AES-256 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as crypto from "crypto"; | ||
|
||
import { PYLON_SECRET } from "../config"; | ||
|
||
// Encrypt function | ||
export function encrypt(text: string): string { | ||
const iv = crypto.randomBytes(16); // Generate a random IV | ||
|
||
console.log(iv.length, PYLON_SECRET.length); | ||
|
||
const cipher = crypto.createCipheriv( | ||
"aes-256-cbc", | ||
Buffer.from(PYLON_SECRET, "hex"), | ||
iv | ||
); | ||
|
||
let encrypted = cipher.update(text, "utf8", "hex"); | ||
encrypted += cipher.final("hex"); | ||
return iv.toString("hex") + encrypted; // Prepend IV to the ciphertext | ||
} | ||
|
||
// Decrypt function | ||
export function decrypt(encryptedText: string): string { | ||
const iv = Buffer.from(encryptedText.slice(0, 32), "hex"); // Extract IV from ciphertext | ||
const decipher = crypto.createDecipheriv( | ||
"aes-256-cbc", | ||
Buffer.from(PYLON_SECRET, "hex"), | ||
iv | ||
); | ||
let decrypted = decipher.update(encryptedText.slice(32), "hex", "utf8"); // Remove IV from ciphertext | ||
decrypted += decipher.final("utf8"); | ||
return decrypted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters