happy-server/sources/modules/encrypt.ts
2025-09-01 15:32:10 -07:00

26 lines
782 B
TypeScript

import { KeyTree, crypto } from "privacy-kit";
let keyTree: KeyTree | null = null;
export async function initEncrypt() {
keyTree = new KeyTree(await crypto.deriveSecureKey({
key: process.env.HANDY_MASTER_SECRET!,
usage: 'happy-server-tokens'
}));
}
export function encryptString(path: string[], string: string) {
return keyTree!.symmetricEncrypt(path, string);
}
export function encryptBytes(path: string[], bytes: Uint8Array) {
return keyTree!.symmetricEncrypt(path, bytes);
}
export function decryptString(path: string[], encrypted: Uint8Array) {
return keyTree!.symmetricDecryptString(path, encrypted);
}
export function decryptBytes(path: string[], encrypted: Uint8Array) {
return keyTree!.symmetricDecryptBuffer(path, encrypted);
}