How dag0310/otp-crypto works
Project type and purpose
• This is an open-source JavaScript/TypeScript library for pseudo one-time-pad encryption of plaintext (otp-crypto).
• It’s packaged as an npm module, tested with Karma/Mocha, and demoed via a static HTML page.
File structure
• Root files: configuration and metadata (.eslintrc.json, package.json, karma.conf.cjs, README.md, LICENSE).
• src/: the library source (otp-crypto.d.ts + otp-crypto.js).
• spec/: unit tests (otp-crypto.spec.js).
• demo/: browser demo (index.html pulls in otp-crypto).
• package.json: declares entry point, dependencies, scripts (lint, test).
Key insights from the README
• API surface: generateRandomBytes(), encrypt(plaintext, key), decrypt(base64, key).
• Data flow: key generation → encryption → Base64 encoding → decryption → remaining-key tracking.
• Demo and tests exercise the same core functions in browser and Node.
a. Core Library Module
• Key Generation Service
• Encryption Engine (XOR logic)
• Base64 Encoder/Decoder
• Decryption Engine
• Key Management (remainingKey flow)
b. Consumer Layers
• Demo Web App (demo/index.html + script tag)
• External Node.js App (npm install + import)
c. Development Tooling
• Test Runner (Karma + Mocha)
• Linter (ESLint)
• TypeScript typings (otp-crypto.d.ts)
d. Packaging & Deployment
• npm registry (package.json publish)
• GitHub Pages for demo
Relationships & interactions
• User calls generateRandomBytes() → Core Key Generation → returns Uint8Array key.
• User calls encrypt(plaintext, key) → Encryption Engine → Base64 Encoder → returns { base64Encrypted, remainingKey }.
• User calls decrypt(base64Encrypted, key) → Base64 Decoder → Decryption Engine → returns { plaintextDecrypted, remainingKey }.
• Demo Web App imports library via <script> or ES module → invokes API.
• Test Runner imports library → exercises API in headless browser/Node environment.
• Linter runs on src/ and spec/ prior to tests.
• package.json scripts wire up lint/test and define entry point for consumers.
Architectural patterns & design principles
• Modular single-responsibility: each sub-module (key gen, encrypt, base64, decrypt) encapsulated within one file.
• Functional API: pure functions that return new state (remainingKey) without side-effects.
• Separation of concerns: core logic decoupled from consumers (demo & tests).
Detailed capture of essential elements
• Data flow of the key and message.
• “remainingKey” feedback loop after encrypt/decrypt.
• Npm registry & GitHub Pages as external endpoints.