UNPKG

tiny-crypto-suite

Version:

Tiny tools, big crypto โ€” seamless encryption and certificate handling for modern web and Node apps.

196 lines (118 loc) โ€ข 4.86 kB
# ๐Ÿ” Tiny Crypto **Tiny Crypto** is a flexible and browser-compatible encryption utility class built for AES-256-GCM encryption with full support for serialization and deserialization of complex JavaScript data types. Whether you're in Node.js or a browser, Tiny Crypto helps you easily encrypt/decrypt values, save/load configurations, and keep your secrets safe โ€” all while supporting real-world usage like RegExp, Date, Buffer, and even DOM elements (in browsers only)! --- ## โœจ Features - ๐Ÿ”’ AES-256-GCM symmetric encryption - ๐Ÿง  Automatic serialization of complex types (Date, RegExp, Set, Map, etc.) - ๐Ÿ“ค Save and load keys/configs from files - ๐ŸŒ Works in both **Node.js** and **Browsers** - โš ๏ธ Type validation on decryption - ๐Ÿ’พ Smart support for file APIs (e.g. `FileReader` in browser, `fs` in Node) --- ## ๐Ÿš€ Getting Started ```js const crypto = new TinyCrypto(); // Encrypt some data const { encrypted, iv, authTag } = crypto.encrypt({ hello: 'world' }); // Decrypt it back const decrypted = crypto.decrypt({ encrypted, iv, authTag }); console.log(decrypted); // { hello: 'world' } ``` --- ## ๐Ÿง  Supported Data Types When you encrypt a value, its type is recorded and restored when decrypted. Supports: - `String`, `Number`, `Boolean`, `BigInt`, `Null`, `Undefined` - `Array`, `Object`, `Map`, `Set`, `Buffer` - `RegExp`, `Date`, `Symbol` - `HTMLElement` _(browser only)_ Does NOT support: - `Function`, `Promise`, `WeakMap`, `WeakSet` โ†’ โŒ Will throw! --- ## ๐Ÿ”ง API ### `constructor(options)` | Option | Type | Default | Description | | ---------------- | ------ | --------------- | --------------------------------- | | `algorithm` | string | `'aes-256-gcm'` | AES algorithm used for encryption | | `key` | Buffer | auto-generated | The secret key to use (32 bytes) | | `outputEncoding` | string | `'hex'` | Encoding used for outputs | | `inputEncoding` | string | `'utf8'` | Encoding used for plain data | | `authTagLength` | number | `16` | GCM auth tag length | --- ### ๐Ÿ”’ `encrypt(data, iv?)` Encrypts a value and returns an object with `{ iv, encrypted, authTag }`. ```js const result = crypto.encrypt('Hello!'); ``` --- ### ๐Ÿ”“ `decrypt({ iv, encrypted, authTag }, expectedType?)` Decrypts a previously encrypted value and returns the original data. You can optionally pass an `expectedType` to validate it. ```js const plain = crypto.decrypt(result, 'string'); ``` --- ### ๐Ÿ”Ž `getTypeFromEncrypted({ iv, encrypted, authTag })` Returns the type name of the encrypted data without fully decrypting it. --- ### ๐Ÿ”‘ `generateKey(length = 32)` Generates a secure random key. Default: 32 bytes (AES-256). --- ### ๐Ÿงฌ `generateIV(length = 12)` Generates a secure random IV. Default: 12 bytes (GCM standard). --- ### ๐Ÿ”‘ `getKey()` This method returns the current cryptographic key used internally by the class. The key is converted to a hexadecimal (hex) string before being returned. --- ### ๐Ÿ”‘ `setKey(keyHex)` This method allows setting a cryptographic key directly. The key should be provided as a string (in hex format) and will be converted to a Buffer for internal use. If the key format is incorrect, an error will be thrown. --- ### ๐Ÿ’พ `saveKeyToFile(filename = 'secret.key')` Saves the current key to a file (browser: prompts download). --- ### ๐Ÿ“‚ `loadKeyFromFile(file)` Loads a key from a file (browser: File object, Node: file path). --- ### ๐Ÿ’พ `saveConfigToFile(filename = 'crypto-config.json')` Saves the current configuration as JSON. --- ### ๐Ÿ“‚ `loadConfigFromFile(file)` Loads configuration from a JSON file. --- ### ๐Ÿ“ฆ `exportConfig()` Returns an object with the current settings: ```json { "algorithm": "aes-256-gcm", "outputEncoding": "hex", "inputEncoding": "utf8", "authTagLength": 16, "key": "..." } ``` --- ### ๐Ÿš€ `importConfig(config)` Applies a configuration object. Throws if invalid types are provided. --- ### ๐Ÿ”ง `setDeepMode(value)` Sets the behavior for deep serialization and deserialization. --- ## ๐Ÿงช Type Validation You can ensure the decrypted value matches the original type: ```js crypto.decrypt(result, 'map'); // โœ… crypto.decrypt(result, 'set'); // โŒ throws if type mismatch ``` --- ## โ— Errors You Might See - `Unsupported data type for encryption: Function` - `Type mismatch: expected Map, but got Set` - `Invalid config JSON file` - `HTMLElement deserialization is only supported in browsers` --- ## ๐Ÿ›ก๏ธ Security Notice This utility is for learning and light usage. For production, ensure: - Proper key management (use secure vaults) - Safe IV reuse (IVs must be unique per encryption) - Your platform's crypto standards are followed