Introduced Crypt Functions
This commit is contained in:
4344
package-lock.json
generated
4344
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -30,6 +30,7 @@
|
|||||||
"archiver": "^7.0.1",
|
"archiver": "^7.0.1",
|
||||||
"axios": "^1.12.1",
|
"axios": "^1.12.1",
|
||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
|
"crypto": "^1.0.1",
|
||||||
"dayjs": "^1.11.18",
|
"dayjs": "^1.11.18",
|
||||||
"fastify": "^5.5.0",
|
"fastify": "^5.5.0",
|
||||||
"fastify-plugin": "^5.0.1",
|
"fastify-plugin": "^5.0.1",
|
||||||
|
|||||||
39
src/utils/crypt.ts
Normal file
39
src/utils/crypt.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import crypto from "crypto";
|
||||||
|
import {secrets} from "./secrets"
|
||||||
|
const ALGORITHM = "aes-256-gcm";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export function encrypt(text) {
|
||||||
|
const ENCRYPTION_KEY = Buffer.from(secrets.ENCRYPTION_KEY, "hex");
|
||||||
|
const iv = crypto.randomBytes(16);
|
||||||
|
const cipher = crypto.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv);
|
||||||
|
|
||||||
|
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]);
|
||||||
|
const tag = cipher.getAuthTag();
|
||||||
|
|
||||||
|
return {
|
||||||
|
iv: iv.toString("hex"),
|
||||||
|
content: encrypted.toString("hex"),
|
||||||
|
tag: tag.toString("hex"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decrypt({ iv, content, tag }) {
|
||||||
|
console.log(iv)
|
||||||
|
const ENCRYPTION_KEY = Buffer.from(secrets.ENCRYPTION_KEY, "hex");
|
||||||
|
const decipher = crypto.createDecipheriv(
|
||||||
|
ALGORITHM,
|
||||||
|
ENCRYPTION_KEY,
|
||||||
|
Buffer.from(iv, "hex")
|
||||||
|
);
|
||||||
|
decipher.setAuthTag(Buffer.from(tag, "hex"));
|
||||||
|
|
||||||
|
const decrypted = Buffer.concat([
|
||||||
|
decipher.update(Buffer.from(content, "hex")),
|
||||||
|
decipher.final(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return decrypted.toString("utf8");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user