26 lines
829 B
TypeScript
26 lines
829 B
TypeScript
import { isEncryptedField, type EncryptedField } from '@bytelyst/field-encrypt';
|
|
import { getEncryptor } from './field-encrypt.js';
|
|
|
|
export async function encryptTextField(
|
|
value: string | undefined,
|
|
userId: string,
|
|
context: string,
|
|
): Promise<string | undefined> {
|
|
if (value === undefined) return undefined;
|
|
if (isEncryptedField(value)) return value as unknown as string;
|
|
|
|
const encrypted = await getEncryptor().encrypt(value, { userId, context });
|
|
return encrypted as unknown as string;
|
|
}
|
|
|
|
export async function decryptTextField(
|
|
value: string | undefined,
|
|
userId: string,
|
|
context: string,
|
|
): Promise<string | undefined> {
|
|
if (value === undefined) return undefined;
|
|
if (!isEncryptedField(value)) return value;
|
|
|
|
return getEncryptor().decrypt(value as unknown as EncryptedField, { userId, context });
|
|
}
|