doublesec
v1.1.0
Published
An independent TypeScript implementation of Keybase TripleSec v4 for browsers and Node.js.
Readme
DoubleSec
DoubleSec is a simple, double-paranoid encryption library and commandline tool. It derives keys with scrypt, encrypts in two layers (XSalsa20 followed by AES-CTR), and authenticates the header, salt, and encrypted body with two tags.
The library is an independent TypeScript implementation of Keybase TripleSec v4, with an asynchronous API for browser and Node.js applications. The library uses native Web Crypto for AES-CTR and HMAC-SHA-512 when supported, and falls back to Noble implementations when those operations are unavailable or blocked. Scrypt, XSalsa20, and HMAC-SHA3-512 use Noble. It does not depend on TripleSec's original source code.
The doublesec commandline utility encrypts and decrypts files from a terminal using the same implementation.
This library is named DoubleSec because the TripleSec v4 format uses only two encryption layers.
Library Usage
The package provides named ESM exports for Node.js and browser applications. Every data and passphrase argument must be a Uint8Array; strings are not accepted directly. Passphrases must contain at least one byte. The library does not assess passphrase strength.
The TypeScript declarations use generic typed-array types and require TypeScript 5.7 or later.
| Function | What it does |
| --------------------------------- | ------------------------------------------------------------------------ |
| encrypt | Encrypts plaintext bytes and returns TripleSec v4 ciphertext bytes. |
| decrypt | Authenticates ciphertext and returns the original plaintext bytes. |
| stringToBytes / bytesToString | Convert between strings and UTF-8 bytes. |
| bytesToBase64 / base64ToBytes | Convert between bytes and standard Base64 text for storage or transport. |
Use the same passphrase bytes to encrypt and decrypt. If starting with a passphrase string, convert it to UTF-8 once and retain or recreate those same bytes when decrypting.
TripleSec v4 ciphertext is always 192 bytes longer than its plaintext. For strings, the plaintext size is the UTF-8 byte length, not the number of characters. Each encryption uses a fresh random salt and initialization vectors, so encrypting identical inputs twice produces different ciphertexts.
Encrypt And Decrypt Text
Encode both the passphrase and message with stringToBytes. decrypt returns bytes; use bytesToString to decode text.
import { bytesToString, decrypt, encrypt, stringToBytes } from 'doublesec';
const passphrase = stringToBytes('replace with a strong passphrase');
const data = stringToBytes('Hello, 世界 👋');
const ciphertext = await encrypt({ passphrase, data });
const decryptedBytes = await decrypt({ passphrase, data: ciphertext });
const plaintext = bytesToString(decryptedBytes);
console.log(plaintext); // Hello, 世界 👋Encrypt And Decrypt Binary Data
For files or other arbitrary data, pass bytes directly and keep the decrypted result as bytes. Do not convert arbitrary binary data to text.
import { decrypt, encrypt } from 'doublesec';
const passphrase = crypto.getRandomValues(new Uint8Array(32)); // Persist securely and reuse for decryption.
const data = Uint8Array.of(0x00, 0x80, 0xff);
const ciphertext = await encrypt({ passphrase, data });
const recovered = await decrypt({ passphrase, data: ciphertext });
// recovered is Uint8Array [0, 128, 255]Convert Ciphertext To A String
Ciphertext is binary. Encode it as a Base64 string when a text representation of the ciphertext is needed, for instance to store it in a database, then decode it before decryption. Base64 is an encoding, not an additional form of encryption.
import {
base64ToBytes,
bytesToBase64,
bytesToString,
decrypt,
encrypt,
stringToBytes,
} from 'doublesec';
const passphrase = stringToBytes('replace with a strong passphrase');
const data = stringToBytes('A message to store');
const ciphertext = await encrypt({ passphrase, data });
const storedValue = bytesToBase64(ciphertext); // Save this string to the database.
const decodedCiphertext = base64ToBytes(storedValue); // Same bytes as ciphertext.
const decryptedBytes = await decrypt({ passphrase, data: decodedCiphertext });
const message = bytesToString(decryptedBytes);Errors and Ciphertext Size
encrypt and decrypt return promises and reject if their byte inputs are invalid or an operation fails. Decryption also rejects malformed or unsupported ciphertext and authentication failures, such as a wrong passphrase or altered data. Handle these errors at the point where you can report or recover from them; do not treat failed decryption as an empty message.
The conversion helpers are synchronous. base64ToBytes throws on malformed Base64, and bytesToString throws on invalid UTF-8.
CLI Usage
The doublesec CLI provides a file-based interface for encrypting and decrypting files from a terminal. It uses the same passphrase-based format and encryption library as the Node.js and browser APIs.
Run the CLI without a separate installation using npx:
npx doublesec encrypt report.pdf -o report.pdf.enc
npx doublesec decrypt report.pdf.enc -o report-restored.pdfIn a terminal, the commands prompt for a passphrase. To supply the passphrase from a file or another command, redirect or pipe it into stdin. For redirected or piped input, the CLI removes one trailing LF or CRLF, preserves all other bytes, and rejects an empty passphrase.
npx doublesec encrypt report.pdf -o report.pdf.enc < passphrase.txt
op read 'op://Private/Doublesec/password' | npx doublesec decrypt report.pdf.enc -o report-restored.pdfThe input file is never changed, and the output path must be new. The CLI streams files in 1 MiB chunks and uses about 32 MiB for scrypt key derivation. The maximum plaintext size is 256 GiB minus 64 bytes. The corresponding ciphertext limit includes an additional 192 bytes. Decryption uses a private temporary file in the output directory to stage the ciphertext, then decrypts it in place after both authentication tags pass. It publishes the output only after decryption completes.
On Ctrl-C or SIGTERM during file processing, the CLI removes its temporary file before exiting. Forced termination can leave the temporary file behind.
Run npx doublesec --help for command syntax.
Development
Use Node.js 22.18+ on the 22.x line, 24.11+ on the 24.x line, or 26+.
pnpm install
pnpm check
pnpm test
pnpm test --coverage
pnpm build
pnpm dev| Command | Purpose |
| ---------------------- | -------------------------------------------------------------- |
| pnpm check | Run formatting, linting, and TypeScript checks. |
| pnpm test | Run the Vitest test suite. |
| pnpm test --coverage | Run tests with V8 coverage. |
| pnpm build | Build the library, classic-script bundle, and standalone demo. |
| pnpm dev | Start the Vite development server. |
| pnpm lint | Run Oxlint only. |
| pnpm fmt | Apply the Prettier-compatible Oxfmt formatter. |
| pnpm fmt --check | Check formatting without changing files. |
Build, lint, formatting, and test settings live in vite.config.ts.
bin/doublesec.jsis the generated CLI.dist/index.jsis the unminified ESM entry, with TypeScript declarations indist/index.d.ts. It has named exports and imports individual Noble modules so application bundlers can tree-shake and minify the final build.dist/doublesec.jsis the standalone browser bundle. Load it with<script src="./dist/doublesec.js"></script>to useglobalThis.DoubleSecwithout a module loader.dist/demo.htmlis a self-contained HTML demo. Open it directly from disk or serve it over HTTP. It makes no network requests.
Browser and Security Notes
The library uses native Web Crypto for AES-CTR and HMAC-SHA-512 when supported, and falls back to Noble implementations when those operations are unavailable or blocked. Scrypt, XSalsa20, and HMAC-SHA3-512 use Noble. Encryption requires crypto.getRandomValues; supported modern browsers and Node.js versions provide it.
TripleSec v4 uses a fixed scrypt cost for compatibility. Key derivation requires roughly 32 MiB of memory per concurrent operation, so avoid starting many encryptions or decryptions at once. Keep passphrases secret and available for future decryption. Encryption does not conceal the plaintext length.
The classic bundle can be loaded with a regular <script> tag; the standalone demo is a self-contained HTML file you can open from disk. Neither requires ES module loading or makes network requests. For sensitive inputs, use a trusted local copy or HTTPS: a page delivered over plain HTTP can be altered before the encryption code runs. Always use a strong password!
Compatibility
The tests use a published Keybase v4 vector to verify byte-for-byte encryption and decryption with and without native browser crypto.
