Add encryption

This commit is contained in:
Tarrgon
2026-06-29 18:04:58 -04:00
parent 3e26ff5450
commit 7b4e057764
13 changed files with 321 additions and 51 deletions
+42
View File
@@ -0,0 +1,42 @@
// Based off this stackoverflow answer: https://stackoverflow.com/a/66476430
// No point reinventing the wheel
import crypto from 'crypto';
export class Encrypter {
private static algorithm = 'aes-256-cbc';
private static key: Buffer;
static initialize(encryptionKey: string) {
this.key = crypto.scryptSync(encryptionKey, 'salt', 32);
}
static encrypt(clearText: string): string {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(this.algorithm, this.key, iv);
const encrypted = cipher.update(clearText, 'utf8', 'hex');
return [
encrypted + cipher.final('hex'),
Buffer.from(iv).toString('hex'),
].join('|');
}
static decrypt(encryptedText: string): string {
const [encrypted, iv] = encryptedText.split('|');
if (!iv) throw new Error('IV not found');
const decipher = crypto.createDecipheriv(
this.algorithm,
this.key,
Buffer.from(iv, 'hex')
);
return decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
}
static hash(clearText: string): string {
return crypto.createHash('sha256', this.key).update(clearText).digest('base64');
}
}