@vatsdev/encryption-decryption
Version:
A TypeScript library for secure encryption and decryption using Google Cloud KMS and Secret Manager
173 lines (164 loc) • 5.41 kB
TypeScript
interface EncryptionFieldConfig {
shouldEncrypt: boolean;
shouldDecrypt: boolean;
shouldHash: boolean;
isObject: boolean;
isArrayOfObjects: boolean;
}
interface KeyMappingConfig {
[key: string]: string;
}
interface KeyMetadata {
locationId: string;
keyRingId: string;
keyId: string;
secretId: string;
encryptedDEK?: Buffer | null;
keyVersion: string | null;
}
interface EncryptedFieldData {
[key: string]: string;
}
interface EncryptedObjectResult {
encryptedData: any;
}
interface KeyDetailsObjectResult {
kmsPath: string;
secretId: string;
secretNamePath: string;
encryptedDEK: Buffer;
}
interface EntityKeyDetails {
locationId: string | null;
keyRingId: string | null;
keyId: string | null;
secretId: string | null;
encryptedDEK?: Buffer | null;
keyVersion: string | null;
}
interface EntityKeyDetailsResult {
kmsPath: string;
secretId: string;
secretNamePath: string;
encryptedDEK?: Buffer | null;
}
interface EncryptObjectParams {
modelName: string;
data: any;
entityKeyDetailsResult: EntityKeyDetailsResult;
}
declare const generateDEK: (length?: number) => Buffer;
declare const encryptField: (fieldName: string, value: string, dek: Buffer) => Promise<EncryptedFieldData | null>;
declare const decryptField: (fieldName: string, data: any, dek: Buffer) => Promise<string | null>;
declare const handleFieldEncryption: (modelName: string, data: any, dek: Buffer) => Promise<any>;
declare const createNewEncryption: (clientName: string) => Promise<KeyDetailsObjectResult>;
declare const createHash: (data: string) => string;
declare class EncryptionService {
private config;
private keyMaping;
constructor();
encryptObject({ modelName, data, entityKeyDetailsResult, }: EncryptObjectParams): Promise<{
encryptedData: any;
encryptedDEK: Buffer;
}>;
private resolveDEKFromEntityKeyDetails;
decryptObject({ modelName, data, entityKeyDetailsResult, }: {
modelName: string;
data: any;
entityKeyDetailsResult: EntityKeyDetailsResult;
}): Promise<{
decryptedData: any;
encryptedDEK?: Buffer;
}>;
}
declare const _default$2: EncryptionService;
interface EncryptDEKResult {
encryptedDEK: Buffer;
kmsPath: string;
}
interface DecryptDEKParams {
encryptedDEKData: Buffer;
kmsPath: string;
}
declare class KMSService {
private readonly client;
private readonly projectId;
private readonly locationId;
private credentials;
private environment;
constructor();
createSecretKeyWithEnv(clientName?: string): string;
private createKeyRingAndKey;
getExistingKMSPath(clientName: string): Promise<string | null>;
encryptDEK(dek: Buffer, clientName?: string): Promise<EncryptDEKResult>;
decryptDEK({ encryptedDEKData, kmsPath }: DecryptDEKParams): Promise<Buffer>;
}
declare const _default$1: KMSService;
interface SecretVersion {
secretName: string;
secretNamePath: string;
}
interface SecretDataWithVersion {
encryptedDEK: Buffer;
kmsPath: string;
secretNamePath: string;
}
interface SecretData {
encryptedDEK: Buffer;
kmsPath: string;
}
declare class SecretManagerService {
private readonly secretManager;
private readonly projectId;
constructor();
getExistingSecretForClient(clientName: string): Promise<string | null>;
getStructuredSecret(secretName: string): Promise<SecretDataWithVersion>;
createSecret(secretId: string, secretData: SecretData): Promise<SecretVersion>;
getSecret(secretNamePath: string): Promise<Buffer>;
}
declare const _default: SecretManagerService;
declare class ConfigurationError extends Error {
code: string;
constructor(message: string, code: string);
}
declare class EncryptionError extends Error {
code: string;
constructor(message: string, code: string);
}
declare class ValidationError extends Error {
code: string;
constructor(message: string, code: string);
}
declare const ErrorCodes: {
CONFIGURATION: {
MISSING_CONFIG: string;
MISSING_ENV_VAR: string;
INITIALIZATION_ERROR: string;
INVALID_CREDENTIALS: string;
};
ENCRYPTION: {
FIELD_ENCRYPTION_ERROR: string;
FIELD_DECRYPTION_ERROR: string;
DEK_RESOLUTION_ERROR: string;
DEK_DECRYPTION_ERROR: string;
DEK_ENCRYPTION_ERROR: string;
SECRET_RETRIEVAL_ERROR: string;
UNKNOWN_ERROR: string;
KEY_DETAILS_ERROR: string;
KEY_VERSION_ERROR: string;
KEY_CREATION_ERROR: string;
CREATION_ERROR: string;
};
VALIDATION: {
MISSING_REQUIRED_FIELD: string;
INVALID_DEK: string;
};
};
interface EncryptionConfig {
encryptedFields: {
[key: string]: {
shouldHash: boolean;
};
};
}
export { ConfigurationError, type EncryptObjectParams, type EncryptedFieldData, type EncryptedObjectResult, type EncryptionConfig, EncryptionError, type EncryptionFieldConfig, _default$2 as EncryptionService, type EntityKeyDetails, type EntityKeyDetailsResult, ErrorCodes, type KeyDetailsObjectResult, type KeyMappingConfig, type KeyMetadata, ValidationError, createHash, createNewEncryption, decryptField, encryptField, generateDEK, handleFieldEncryption, _default$1 as kmsService, _default as secretManagerService };