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
Markdown
# ๐ 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