Problem
Input length validation, hex normalization, and key format assertions are duplicated with inconsistent error codes and messages across 80+ cipher files.
Detailed Problem Description
Across lib/cipher/classical/, lib/cipher/symmetric/, lib/cipher/asymmetric/, and lib/cipher/hash/, input checking is reimplemented ad-hoc in almost every file:
- Some check
if (!input) throw new CipherError('INPUT_REQUIRED', ...)
- Others check
if (input === undefined || input === null || input === '')
- Some check
input.length > 4096 with error code 'INPUT_TOO_LONG', others with 'INPUT_LIMIT_EXCEEDED'
- Hex cleaning is implemented with varying regexes (
replace(/\s+/g, '') vs replace(/[^0-9a-fA-F]/g, ''))
- Key length errors report different phrasing (
"Invalid key length", "Key must be 16 bytes", "Wrong key size").
This duplication increases maintenance overhead and leads to inconsistent diagnostic messages in the UI.
Current Implementation
lib/cipher/classical/*.ts
lib/cipher/symmetric/*.ts
lib/cipher/asymmetric/*.ts
lib/utils/errors.ts
Why This Should Be Improved
- Maintainability: Centralizes validation logic into reusable helper functions conforming to
GUIDELINES.md error specifications.
- Consistency: Guarantees identical error codes and friendly diagnostic messages across all algorithms.
Proposed Solution
- Create
lib/utils/cipherValidation.ts with standard validation primitives:
validateRequiredInput(input: string, maxLength?: number): void
parseAndValidateHex(hexString: string, expectedByteLength?: number, fieldName?: string): Uint8Array
validateKeyLength(key: string | Uint8Array, allowedLengths: number[], cipherName: string): void
normalizeAsciiText(text: string, options?: { uppercase?: boolean; stripNonAlpha?: boolean }): string
- Refactor cipher modules to import and use these shared helpers.
- Ensure all validation helpers throw standard
CipherError instances with typed CipherErrorCode values.
Affected Files
lib/utils/cipherValidation.ts (new file)
lib/cipher/classical/caesar.ts
lib/cipher/classical/vigenere.ts
lib/cipher/classical/playfair.ts
lib/cipher/symmetric/aes.ts
lib/cipher/symmetric/des.ts
lib/cipher/symmetric/blowfish.ts
tests/unit/utils/cipherValidation.test.ts (new file)
Affected Components
cipherValidation
- All refactored cipher modules
User Experience / Contributor Experience
Users receive consistent, clear error messages across all ciphers. Contributors writing new ciphers can validate inputs with clean, one-line helper calls.
Mathematical Considerations
Not applicable.
Technical Considerations
All helpers must be pure functions with zero DOM or browser dependencies so they run safely inside Web Workers and Node/Vitest test environments.
Proposed Tests
- Verify
validateRequiredInput rejects empty strings and enforces the 4096-byte limit.
- Verify
parseAndValidateHex handles odd lengths and invalid hex characters consistently.
- Verify
validateKeyLength provides clear, readable expected-length messages.
Acceptance Criteria
Problem
Input length validation, hex normalization, and key format assertions are duplicated with inconsistent error codes and messages across 80+ cipher files.
Detailed Problem Description
Across
lib/cipher/classical/,lib/cipher/symmetric/,lib/cipher/asymmetric/, andlib/cipher/hash/, input checking is reimplemented ad-hoc in almost every file:if (!input) throw new CipherError('INPUT_REQUIRED', ...)if (input === undefined || input === null || input === '')input.length > 4096with error code'INPUT_TOO_LONG', others with'INPUT_LIMIT_EXCEEDED'replace(/\s+/g, '')vsreplace(/[^0-9a-fA-F]/g, ''))"Invalid key length","Key must be 16 bytes","Wrong key size").This duplication increases maintenance overhead and leads to inconsistent diagnostic messages in the UI.
Current Implementation
lib/cipher/classical/*.tslib/cipher/symmetric/*.tslib/cipher/asymmetric/*.tslib/utils/errors.tsWhy This Should Be Improved
GUIDELINES.mderror specifications.Proposed Solution
lib/utils/cipherValidation.tswith standard validation primitives:validateRequiredInput(input: string, maxLength?: number): voidparseAndValidateHex(hexString: string, expectedByteLength?: number, fieldName?: string): Uint8ArrayvalidateKeyLength(key: string | Uint8Array, allowedLengths: number[], cipherName: string): voidnormalizeAsciiText(text: string, options?: { uppercase?: boolean; stripNonAlpha?: boolean }): stringCipherErrorinstances with typedCipherErrorCodevalues.Affected Files
lib/utils/cipherValidation.ts(new file)lib/cipher/classical/caesar.tslib/cipher/classical/vigenere.tslib/cipher/classical/playfair.tslib/cipher/symmetric/aes.tslib/cipher/symmetric/des.tslib/cipher/symmetric/blowfish.tstests/unit/utils/cipherValidation.test.ts(new file)Affected Components
cipherValidationUser Experience / Contributor Experience
Users receive consistent, clear error messages across all ciphers. Contributors writing new ciphers can validate inputs with clean, one-line helper calls.
Mathematical Considerations
Not applicable.
Technical Considerations
All helpers must be pure functions with zero DOM or browser dependencies so they run safely inside Web Workers and Node/Vitest test environments.
Proposed Tests
validateRequiredInputrejects empty strings and enforces the 4096-byte limit.parseAndValidateHexhandles odd lengths and invalid hex characters consistently.validateKeyLengthprovides clear, readable expected-length messages.Acceptance Criteria
lib/utils/cipherValidation.tsexports standard validation utilities.INPUT_REQUIRED,INPUT_TOO_LONG,INVALID_KEY) are uniformly emitted.