3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 00:05:01 +00:00

perf: move encryption/decryption to a separate thread

This commit is contained in:
Dragory 2021-10-09 14:21:23 +03:00
parent 0b337a13a4
commit b7c7e002eb
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
16 changed files with 310 additions and 147 deletions

View file

@ -0,0 +1,31 @@
import crypto from "crypto";
import { expose } from "threads";
const ALGORITHM = "aes-256-gcm";
function encrypt(str, key) {
// Based on https://gist.github.com/rjz/15baffeab434b8125ca4d783f4116d81
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
let encrypted = cipher.update(str, "utf8", "base64");
encrypted += cipher.final("base64");
return `${iv.toString("base64")}.${cipher.getAuthTag().toString("base64")}.${encrypted}`;
}
function decrypt(encrypted, key) {
// Based on https://gist.github.com/rjz/15baffeab434b8125ca4d783f4116d81
const [iv, authTag, encryptedStr] = encrypted.split(".");
const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, "base64"));
decipher.setAuthTag(Buffer.from(authTag, "base64"));
let decrypted = decipher.update(encryptedStr, "base64", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
const toExpose = { encrypt, decrypt };
expose(toExpose);
export type CryptFns = typeof toExpose;