tiny-crypto-suite
Version:
Tiny tools, big crypto β seamless encryption and certificate handling for modern web and Node apps.
105 lines (66 loc) β’ 2.73 kB
Markdown
# β¨ TinyOlm
TinyOlm is a minimal and powerful wrapper around [Olm](https://gitlab.matrix.org/matrix-org/olm), designed to handle secure end-to-end encryption for Matrix-compatible environments β but small enough to fit in any project π‘οΈ
Whether you're building a chat app, a P2P network, or just need secure encryption between devices, **TinyOlm** gives you the essentials: identity management, session handling, and message encryption β all wrapped in a compact and intuitive interface.
> β οΈ **Important:** All encryption and decryption operations are automatically **queued and executed in order** using an internal `TinyPromiseQueue` from module `tiny-essentials`.
> This ensures that all cryptographic requests are **processed in the correct sequence**, so you don't need to handle the ordering manually.
## π Features
- π Identity & Device Key Management
- π One-Time & Session Key Generation
- π¦ Lightweight API for encrypting/decrypting messages
- π§ͺ Supports both structured and raw message formats
- π Fully compatible with Olm (WebAssembly)
## π Basic Encryption
```js
const encrypted = tinyOlm.encrypt('alice', { hello: 'world' });
```
## π Basic Decryption
```js
const message = tinyOlm.decrypt('alice', 1, encrypted.body);
```
## π οΈ How to Use
TinyOlm works through a class-based instance system. Every device gets its own `TinyOlm.Instance`, which manages your encryption identity, sessions, and storage π
### β
Step-by-step Setup
#### 1. Import and create your instance
```js
import { TinyOlm } from 'tiny-crypto-suite';
const olm = new TinyOlm.Instance('@pudding', 'DEVICE123', 'optional-password');
```
#### 2. Initialize the library and create your account
```js
await olm.init(); // Load Olm and create a new identity
```
#### 3. (Optional) Load saved sessions from IndexedDB
```js
await olm.initIndexedDb(); // Load identity + restore sessions from the browser
```
### π§ͺ Basic Encryption
To encrypt any message or object:
```js
const encrypted = olm.encrypt('@friend', { hello: 'π' });
```
This returns:
```js
{
type: 1,
body: 'ENCRYPTED_PAYLOAD'
}
```
### π§© Basic Decryption
To decrypt a received message:
```js
const message = olm.decrypt('@friend', 1, encrypted.body);
console.log(message); // β { hello: 'π' }
```
You can also decrypt raw plaintexts (from `encryptMessage`) or expect a specific type for validation.
### π§ What's Stored?
Once `initIndexedDb()` is called, TinyOlm automatically stores and restores:
- Your account identity
- One-to-one sessions
- Group sessions (inbound + outbound)
Making it a persistent and browser-safe encryption solution πΎπ