Matheus Sampaio Queiroga
228d28bc22
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
18 lines
1.0 KiB
TypeScript
18 lines
1.0 KiB
TypeScript
import crypto from "node:crypto";
|
|
import util from "node:util";
|
|
|
|
const scrypt = util.promisify(crypto.scrypt) as (password: crypto.BinaryLike, salt: crypto.BinaryLike, keylen: number) => Promise<Buffer>;
|
|
|
|
export async function encrypt(text: string): Promise<string> {
|
|
const iv = crypto.randomBytes(16), secret = crypto.randomBytes(24);
|
|
const key = await scrypt(secret, "salt", 24);
|
|
const cipher = crypto.createCipheriv("aes-192-cbc", key, iv); cipher.on("error", () => {});
|
|
return (String()).concat(iv.toString("hex"), secret.toString("hex"), cipher.update(text, "utf8", "hex"), cipher.final("hex"));
|
|
}
|
|
|
|
export async function decrypt(hash: string): Promise<string> {
|
|
const iv = Buffer.from(hash.substring(0, 32), "hex"), secret = Buffer.from(hash.substring(32, 80), "hex"), cipher = hash.substring(80);
|
|
const key = await scrypt(secret, "salt", 24);
|
|
const decipher = crypto.createDecipheriv("aes-192-cbc", key, iv); decipher.on("error", () => {});
|
|
return (String()).concat(decipher.update(cipher, "hex", "utf8"), decipher.final("utf8"));
|
|
} |