api: flatten code directories, better filenames, remove old files

This commit is contained in:
wukko
2024-08-03 14:47:13 +06:00
parent 5ce208f1a5
commit dd831e13e8
53 changed files with 83 additions and 249 deletions

27
api/src/misc/crypto.js Normal file
View File

@@ -0,0 +1,27 @@
import { createHmac, createCipheriv, createDecipheriv, randomBytes } from "crypto";
const algorithm = "aes256";
export function generateSalt() {
return randomBytes(64).toString('hex');
}
export function generateHmac(str, salt) {
return createHmac("sha256", salt).update(str).digest("base64url");
}
export function encryptStream(plaintext, iv, secret) {
const buff = Buffer.from(JSON.stringify(plaintext));
const key = Buffer.from(secret, "base64url");
const cipher = createCipheriv(algorithm, key, Buffer.from(iv, "base64url"));
return Buffer.concat([ cipher.update(buff), cipher.final() ])
}
export function decryptStream(ciphertext, iv, secret) {
const buff = Buffer.from(ciphertext);
const key = Buffer.from(secret, "base64url");
const decipher = createDecipheriv(algorithm, key, Buffer.from(iv, "base64url"));
return Buffer.concat([ decipher.update(buff), decipher.final() ])
}