Skip to content

Shared Input/Key Parser & Validator Pipeline with Structured Feedback #1164

Description

@csxark

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

  1. 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
  2. Refactor cipher modules to import and use these shared helpers.
  3. 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

  • lib/utils/cipherValidation.ts exports standard validation utilities.
  • Core ciphers utilize shared validation helpers.
  • Standardized error codes (INPUT_REQUIRED, INPUT_TOO_LONG, INVALID_KEY) are uniformly emitted.

Metadata

Metadata

Assignees

Labels

ECSoC26Elite Coders Summer of Code 2026

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions