wh-encrypt-api
Version:
Cross-platform encryption library providing consistent AES-256-GCM encryption for Node.js and Browser environments
101 lines (87 loc) • 2.82 kB
TypeScript
export interface EncryptionConstants {
ALGORITHM: string;
KEY_LENGTH: number;
IV_LENGTH: number;
TAG_LENGTH: number;
}
export interface EncryptionInstance {
/**
* Encrypt data using AES-256-GCM
* @param data - Data to encrypt (will be JSON stringified)
* @returns Base64 encoded encrypted data with IV and auth tag
*/
encryptData(data: any): string;
/**
* Decrypt data using AES-256-GCM
* @param encryptedData - Base64 encoded encrypted data
* @returns Decrypted and parsed data
*/
decryptData(encryptedData: string): any;
/**
* Safely decrypt request payload
* @param payload - Request payload
* @returns Decrypted data or original payload if not encrypted
*/
decryptRequest(payload: any): any;
/**
* Encrypt response payload for consistent API responses
* @param data - Response data to encrypt
* @returns Encrypted response object with metadata
*/
encryptResponse(data: any): {
encrypted: true;
data: string;
timestamp: string;
};
/**
* Validate encryption configuration
* @returns True if configuration is valid
*/
validateConfig(): boolean;
/**
* Constants for external use
*/
constants: EncryptionConstants;
}
export interface BrowserEncryptionInstance {
/**
* Encrypt data using Web Crypto API AES-GCM
* @param data - Data to encrypt (will be JSON stringified)
* @returns Promise that resolves to Base64 encoded encrypted data
*/
encryptData(data: any): Promise<string>;
/**
* Decrypt data using Web Crypto API AES-GCM
* @param encryptedData - Base64 encoded encrypted data
* @returns Promise that resolves to decrypted and parsed data
*/
decryptData(encryptedData: string): Promise<any>;
/**
* Check if the current browser supports Web Crypto API
* @returns True if Web Crypto API is supported
*/
isCryptoSupported(): boolean;
/**
* Constants for external use
*/
constants: EncryptionConstants;
}
/**
* Initialize the encryption library with a secret key
* @param secretKey - The secret key for encryption/decryption
* @param salt - Salt for key derivation (default: 'encryption-salt')
* @returns Encryption/decryption functions
*/
export function createEncryption(secretKey: string, salt?: string): EncryptionInstance;
/**
* Browser-compatible encryption for frontend usage
* @param secretKey - The secret key for encryption/decryption
* @param salt - Salt for key derivation (default: 'encryption-salt')
* @returns Browser-compatible encryption functions
*/
export function createBrowserEncryption(secretKey: string, salt?: string): BrowserEncryptionInstance;
declare const _default: {
createEncryption: typeof createEncryption;
createBrowserEncryption: typeof createBrowserEncryption;
};
export default _default;