import NativeDesMachine, { type DesMachineParams } from './NativeDesMachine'; import type { ModeEncryptionType, PaddingEncryptionType, OutputFormatType, } from './NativeDesMachine'; // Constants const Padding: Record = { PKCS7: 'PKCS7', ISO10126: 'ISO10126', ZERO: 'ZERO', NONE: 'NONE', }; const Mode: Record = { ECB: 'ECB', CBC: 'CBC', CFB: 'CFB', OFB: 'OFB', CTR: 'CTR', }; const Format: Record = { BASE64: 'BASE64', HEX: 'HEX', }; // Options for UI interface OptionsList { label: string; value: T; } const modeOptions: OptionsList[] = [ { label: 'ECB (Electronic Codebook)', value: Mode.ECB }, { label: 'CBC (Cipher Block Chaining)', value: Mode.CBC }, { label: 'CFB (Cipher Feedback)', value: Mode.CFB }, { label: 'OFB (Output Feedback)', value: Mode.OFB }, { label: 'CTR (Counter)', value: Mode.CTR }, ]; const paddingOptions: OptionsList[] = [ { label: 'PKCS7', value: Padding.PKCS7 }, { label: 'ISO10126', value: Padding.ISO10126 }, { label: 'Zero Padding', value: Padding.ZERO }, { label: 'No Padding', value: Padding.NONE }, ]; const formatOptions: OptionsList[] = [ { label: 'Base64', value: Format.BASE64 }, { label: 'Hexadecimal', value: Format.HEX }, ]; // Default parameters const defaultParams: Omit = { mode: Mode.ECB, padding: Padding.PKCS7, outputFormat: Format.BASE64, }; /** * DES Machine class for encryption and decryption. * Supports multiple instances with different configurations. * * @example * ```typescript * // Create a machine with default settings * const machine = new DesMachine({ key: 'mySecretKey123' }); * * // Encrypt and decrypt * const encrypted = machine.encrypt('Hello World'); * const decrypted = machine.decrypt(encrypted); * * // Create another machine with custom IV for CBC mode * const cbcMachine = new DesMachine({ * key: 'anotherKey1', * iv: '12345678', // 8 characters for DES * mode: Mode.CBC, * outputFormat: Format.HEX, * }); * ``` */ class DesMachine { private params: DesMachineParams; /** * Create a new DES Machine instance. * @param params - Configuration parameters * @throws Error if key is missing or less than 8 characters * @throws Error if iv is provided but not exactly 8 characters */ constructor(params: DesMachineParams) { if (!params.key) { throw new Error('DesMachine: key is required'); } if (params.key.length < 8) { throw new Error('DesMachine: key must be at least 8 characters long'); } if (params.iv !== undefined && params.iv.length !== 8) { throw new Error('DesMachine: iv must be exactly 8 characters long'); } this.params = { key: params.key, iv: params.iv, mode: params.mode ?? defaultParams.mode!, padding: params.padding ?? defaultParams.padding!, outputFormat: params.outputFormat ?? defaultParams.outputFormat!, }; } /** * Encrypt plaintext using DES algorithm. * @param text - The plaintext to encrypt * @returns Encrypted string in the configured output format */ encrypt(text: string): string { return NativeDesMachine.encrypt(this.params, text); } /** * Decrypt ciphertext using DES algorithm. * @param text - The encrypted string to decrypt * @returns Decrypted plaintext */ decrypt(text: string): string { return NativeDesMachine.decrypt(this.params, text); } /** * Get the current configuration parameters. * @returns A copy of the current parameters (key and iv are masked) */ getParams(): Omit & { key: string; iv?: string; } { return { ...this.params, key: this.params.key.substring(0, 2) + '***', iv: this.params.iv ? this.params.iv.substring(0, 2) + '***' : undefined, }; } /** * Update configuration parameters. * @param params - New parameters to apply (key and iv cannot be changed) */ updateParams(params: Partial>): void { if (params.mode) this.params.mode = params.mode; if (params.padding) this.params.padding = params.padding; if (params.outputFormat) this.params.outputFormat = params.outputFormat; } } /** * Create a new DES Machine instance. * @param params - Configuration parameters * @returns A new DesMachine instance * * @example * ```typescript * const machine = createDesMachine({ key: 'mySecretKey123' }); * const encrypted = machine.encrypt('Hello'); * ``` */ function createDesMachine(params: DesMachineParams): DesMachine { return new DesMachine(params); } /** * Quick encrypt function for one-off encryption. * @param params - Configuration parameters including key * @param text - The plaintext to encrypt * @returns Encrypted string * * @example * ```typescript * const encrypted = encrypt({ key: 'mySecretKey123' }, 'Hello World'); * ``` */ function encrypt(params: DesMachineParams, text: string): string { const machine = new DesMachine(params); return machine.encrypt(text); } /** * Quick decrypt function for one-off decryption. * @param params - Configuration parameters including key * @param text - The encrypted string to decrypt * @returns Decrypted plaintext * * @example * ```typescript * const decrypted = decrypt({ key: 'mySecretKey123' }, encryptedText); * ``` */ function decrypt(params: DesMachineParams, text: string): string { const machine = new DesMachine(params); return machine.decrypt(text); } export { // Main class DesMachine, createDesMachine, // Quick functions encrypt, decrypt, // Constants Mode, Padding, Format, // Options for UI modeOptions, paddingOptions, formatOptions, }; export type { DesMachineParams, ModeEncryptionType, PaddingEncryptionType, OutputFormatType, };