UNPKG

tiny-crypto-suite

Version:

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

274 lines (167 loc) β€’ 8.98 kB
# πŸ” TinyOlm IndexedDB Encryption Mode This tutorial provides a info around the TinyOlm API to perform secure encryption and decryption using only **IndexedDB**. It includes support for both **individual Olm sessions** and **group Megolm sessions**, with all cryptographic sessions serialized and retrieved directly from `indexedDB`. ## πŸ” Local Storage Mode To use the IndexedDB-based encryption mode, make sure to **disable the default local memory session system**: ```js tinyOlm.setUseLocal(false); ``` This is required so that all session operations (such as loading or saving sessions) will exclusively use IndexedDB instead of in-memory storage. This ensures persistence across browser reloads and isolates the module for more consistent and stateful encryption behavior. --- ## πŸ“¦ Features This module offers the following methods for secure messaging and data exchange: ### 🧍 One-to-One Communication - `encryptMessageV2(toUsername, plaintext)` - Encrypts a plaintext string for a specific user using an existing Olm session from IndexedDB. - `decryptMessageV2(fromUsername, messageType, ciphertext)` - Decrypts an encrypted message received from a specific user. - `encryptV2(toUsername, data)` - Encrypts any content (object, number, etc.) to a user. - `decryptV2(fromUsername, messageType, plaintext, expectedType?)` - Decrypts content and optionally verifies the data type after deserialization. ### πŸ‘₯ Group Communication (Megolm) - `encryptGroupMessageV2(roomId, plaintext)` - Encrypts a plaintext string using the outbound group session for a room. - `decryptGroupMessageV2(roomId, userId, encryptedMessage)` - Decrypts a group message using the appropriate inbound session from IndexedDB. - `encryptGroupContentV2(roomId, data)` - Encrypts any structured content (not just strings) for a group session. - `decryptGroupContentV2(roomId, userId, encryptedMessage, expectedType?)` - Decrypts and optionally verifies the type of decrypted group content. --- ## ⚠️ Requirements Before calling any of the encryption/decryption methods: - You **must** already have a pickled session stored in IndexedDB. - This module assumes session management (creation and storage) has been done ahead of time. --- ## 🧠 Note All encryption and decryption operations internally: - Retrieve the session from IndexedDB - Unpickle the session with a password - Free the session memory securely after use --- ## πŸ’¬ Example ```js await tinyOlm.setUseLocal(false); // Required const encrypted = await myModule.encryptMessageV2('user123', 'Hello there!'); const decrypted = await myModule.decryptMessageV2('user123', 1, encrypted.body); ``` --- ### πŸ”„ `setUseLocal(value: boolean): void` Enables or disables **in-memory storage**. When set to `true`, operations will run using volatile memory with persistent storage. Ideal for tests πŸ§ͺ or temporary sessions that don't need to be saved. - **Parameter**: - `value` (`boolean`): `true` to enable memory mode 🧠, `false` to disable it. --- ### ❓ `isUseLocal(): boolean` Checks whether the system is currently using **in-memory storage**. - **Returns**: `boolean` β€” `true` if memory mode is active 🟒, `false` otherwise πŸ”΄. --- ### πŸ“¦ `exportDbSession(userId: string): Promise<string>` Exports a specific **Olm session** πŸ” from `indexedDb` for a given user. - **Parameter**: - `userId` (`string`): The remote device’s user ID πŸ‘€. - **Returns**: `Promise<string>` β€” The serialized (pickled) session 🧴. - **Throws**: `Error` if the session cannot be found ❌. --- ### πŸ§‘β€πŸ€β€πŸ§‘ `exportDbGroupSession(roomId: string): Promise<string>` Exports an **outbound group session** πŸ“€ from `indexedDb` for a specific room. - **Parameter**: - `roomId` (`string`): The room’s unique ID 🏠. - **Returns**: `Promise<string>` β€” The pickled outbound group session. - **Throws**: `Error` if the session does not exist ❗. --- ### πŸ” `exportDbInboundGroupSession(roomId: string, userId: string): Promise<string>` Exports an **inbound group session** πŸ“₯ from `indexedDb`, filtered by room and sender. - **Parameters**: - `roomId` (`string`): The room ID 🏠. - `userId` (`string`): The sender’s user ID πŸ‘€. - **Returns**: `Promise<string>` β€” The serialized inbound group session. - **Throws**: `Error` if the session is missing ⚠️. --- ### πŸ“€ `exportDbInstance(): Promise<ExportedOlmInstance>` Exports the entire Olm data instance πŸ“š from `indexedDb`, including: - Account - Sessions - Outbound group sessions - Inbound group sessions - **Returns**: `Promise<ExportedOlmInstance>` β€” A full export of the Olm structure 🧩. --- ### πŸ—οΈ `exportDbGroupSessionId(roomId: string): Promise<string>` Retrieves the **session key** πŸ”‘ of the current outbound group session for a room in the `indexedDb`. - **Parameter**: - `roomId` (`string`): The room’s ID 🏠. - **Returns**: `Promise<string>` β€” The active session key. - **Throws**: `Error` if no outbound session exists for the room πŸ›‘. --- ### πŸ” `encryptMessageV2(toUsername: string, plaintext: string): Promise<EncryptedMessage>` Encrypts a **plaintext message** πŸ“ to a specific user using their stored session. - **Parameters**: - `toUsername` (`string`): Recipient’s user ID πŸ‘€ - `plaintext` (`string`): The message content to encrypt βœ‰οΈ - **Returns**: `Promise<EncryptedMessage>` β€” Encrypted message object πŸ”’ - **Throws**: `Error` if session with the user does not exist ❌ --- ### πŸ”“ `decryptMessageV2(fromUsername: string, messageType: number, ciphertext: string): Promise<string>` Decrypts a **received message** 🧾 from a specific user. - **Parameters**: - `fromUsername` (`string`): Sender’s user ID πŸ‘€ - `messageType` (`number`): `0` for pre-key πŸ”‘, `1` for message πŸ’¬ - `ciphertext` (`string`): Encrypted message πŸ” - **Returns**: `Promise<string>` β€” Decrypted message πŸ“¬ - **Throws**: `Error` if session with the user is missing 🚫 --- ### πŸ“¦ `encryptV2(toUsername: string, data: any): Promise<EncryptedMessage>` Encrypts **any data** 🧠 to a specific user. - **Parameters**: - `toUsername` (`string`): Recipient’s user ID πŸ‘€ - `data` (`any`): Serializable data to encrypt πŸ—ƒοΈ - **Returns**: `Promise<EncryptedMessage>` β€” Encrypted package πŸ”’ - **Throws**: `Error` if no session is found ❗ --- ### πŸ§ͺ `decryptV2(fromUsername: string, messageType: number, plaintext: string, expectedType?: string|null): Promise<any>` Decrypts serialized content πŸ“„ and optionally checks its type. - **Parameters**: - `fromUsername` (`string`): Sender’s user ID πŸ‘€ - `messageType` (`number`): 0 (pre-key) or 1 (message) πŸ› οΈ - `plaintext` (`string`): Encrypted payload πŸ” - `expectedType` (`string|null`, optional): Validates expected object type 🧩 - **Returns**: `Promise<any>` β€” Decrypted value πŸ“¦ - **Throws**: `Error` if type mismatch or session missing ⚠️ --- ### 🏠 `encryptGroupMessageV2(roomId: string, plaintext: string): Promise<EncryptedData>` Encrypts a plaintext message πŸ“§ for a room using its outbound group session. - **Parameters**: - `roomId` (`string`): The target room ID 🏠 - `plaintext` (`string`): Message to encrypt πŸ“ - **Returns**: `Promise<EncryptedData>` β€” Encrypted message πŸ”’ - **Throws**: `Error` if no group session exists 🚫 --- ### πŸ—£οΈ `decryptGroupMessageV2(roomId: string, userId: string, encryptedMessage: EncryptedData): Promise<DecryptedGroupMessage>` Decrypts a **group message** πŸ“¬ using an inbound session. - **Parameters**: - `roomId` (`string`): Room ID 🏠 - `userId` (`string`): Sender’s user ID πŸ‘€ - `encryptedMessage` (`EncryptedData`): Encrypted payload πŸ” - **Returns**: `Promise<DecryptedGroupMessage>` β€” Decrypted group message πŸ“­ - **Throws**: `Error` if inbound session is not found ❌ --- ### πŸ—ƒοΈ `encryptGroupContentV2(roomId: string, data: any): Promise<EncryptedData>` Encrypts content πŸ”§ for a room using group encryption. - **Parameters**: - `roomId` (`string`): Room ID 🏠 - `data` (`any`): Serializable data πŸ“„ - **Returns**: `Promise<EncryptedData>` β€” Encrypted data string πŸ”’ - **Throws**: `Error` if outbound session is missing ❗ --- ### πŸ“₯ `decryptGroupContentV2(roomId: string, userId: string, encryptedMessage: EncryptedData, expectedType?: string|null): Promise<DecryptedGroupContent>` Decrypts serialized group content 🧾 with optional type checking. - **Parameters**: - `roomId` (`string`): Room ID 🏠 - `userId` (`string`): Sender ID πŸ‘€ - `encryptedMessage` (`EncryptedData`): Encrypted input πŸ” - `expectedType` (`string|null`, optional): Validate expected object type 🎯 - **Returns**: `Promise<DecryptedGroupContent>` β€” Decrypted and validated content βœ… - **Throws**: `Error` if session missing or type mismatch πŸ›‘