libsodium-wrappers

WebAssembly build of libsodium with hand-friendly JavaScript wrappers for encryption, signing, hashing, and key exchange.

Library
npm
v0.8.4
1,153stars
ISC

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
63/100Good
Development Activity44
Maintenance40
Community80
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture88
Code Quality82
Innovation78
Learning Curve80

libsodium-wrappers is the npm distribution of libsodium.js, which compiles the C libsodium cryptographic library to WebAssembly (with a pure-JS asm.js fallback) and layers on JavaScript-friendly wrappers around the raw C API. Instead of manually managing buffer lengths and memory addresses, callers pass and receive Uint8Array objects (or plain strings, auto-converted to UTF-8 bytes), and the library exposes the same function surface as libsodium itself: secret-key and public-key encryption, digital signatures, password hashing, key derivation, generic hashing, and streaming AEAD constructions.

The wrapper functions themselves are not hand-written. They are generated from a set of ~256 JSON symbol definitions (one per libsodium function) that declare each function’s inputs, outputs, and the low-level WASM call to make, which a build script (build-wrappers.ts) turns into the final wrap-template.js output. This same symbol data also drives the generated TypeScript definitions and API docs, keeping the JS surface, type signatures, and documentation in lock-step with the underlying C library as it evolves.

Because the package ships both a dist/modules (CommonJS) and dist/modules-esm (ESM) build, along with browser bundles in dist/browsers, it works the same way in Node.js, Bun, and directly in a <script> tag. A .ready promise gates access to the cryptographic functions, since the underlying WebAssembly module has to finish instantiating asynchronously before any crypto_* calls or constants become available.

What You Get

  • The full high-level libsodium API surface (secretbox, box, sign, generichash, pwhash, kdf, kx, secretstream, AEAD ciphers) exposed as plain async-ready JS functions
  • Automatic conversion between UTF-8 strings and Uint8Array buffers, so callers rarely need manual encoding
  • Both CommonJS (dist/modules) and ESM (dist/modules-esm) builds, plus standalone browser bundles, from one package
  • A WebAssembly implementation with an automatic asm.js fallback for environments where WASM fails to load
  • A sumo variant (published separately as libsodium-wrappers-sumo) exposing the complete, less-common symbol set including crypto_pwhash_*
  • Generated TypeScript type definitions kept in sync with the underlying symbol definitions used to build the wrappers

Common Use Cases

  • End-to-end encrypting messages or files in a web or Node.js app using crypto_secretbox_easy/crypto_box_easy without needing a native crypto addon
  • Signing and verifying data (e.g. license tokens, update manifests) with Ed25519 via crypto_sign_*
  • Deriving and stretching keys from user passwords with crypto_pwhash for local encryption or key-wrapping
  • Building streaming encrypted transports with crypto_secretstream_xchacha20poly1305_* for chunked message encryption with a single header
  • Running the same cryptographic code path in the browser, Node.js, and Bun by sharing one wrapper import across platforms

Under The Hood

Architecture The project separates a thin runtime shim from a much larger code-generation layer. At runtime, wrap-template.js wraps an already-instantiated Emscripten WebAssembly module (libsodium), converts JS-friendly arguments into WASM heap writes, calls into the compiled C functions, and converts the results back into Uint8Array outputs. A .ready promise gates function availability until the WASM module (or its asm.js fallback, triggered automatically if a self-test using crypto_secretbox_easy fails) has finished loading. Every one of the ~256 exposed functions is described declaratively in wrapper/symbols/*.json (name, typed inputs/outputs, buffer lengths as libsodium C-call expressions, the WASM call itself, and return-value assertions), and wrapper/build-wrappers.ts reads these definitions plus a library of small JS “macro” snippets (wrapper/macros/) to generate the actual wrapper source injected into wrap-template.js/wrap-esm-template.js at build time. This keeps the hand-maintained surface small (build scripts and templates) while the large, error-prone buffer-marshaling code is generated rather than written by hand, and TypeScript defs and API docs are generated from the same symbol data so they cannot drift from the runtime implementation.

Tech Stack The published libsodium-wrappers package is a thin wrapper around the libsodium npm package (its only runtime dependency, pinned to ^0.8.0), which contains the actual Emscripten-compiled WebAssembly/asm.js binary produced from the upstream C libsodium library (included as a git submodule). Building from source uses Bun as the JS runtime/test runner, Make to orchestrate the multi-step build (compile via Emscripten/binaryen, generate wrappers, generate docs, generate TypeScript defs, minify with Terser), and Biome for linting/formatting (tabs, double quotes, import-organizing) in place of ESLint/Prettier. There is no bundler involved in the published artifact itself — dist/ ships pre-built CJS, ESM, and browser bundles directly.

Code Quality The repository has an extensive test/ directory (nearly 5,000 lines across ~28 files) using Bun’s built-in test runner, with one file per primitive family (secretbox.test.ts, sign.test.ts, pwhash.test.ts, secretstream.test.ts, kx.test.ts, etc.) plus dedicated files for cross-cutting concerns like ESM interop, symbol enumeration, TypeScript type shapes, and state-address handling. Tests exercise both success paths and expected-failure/edge-case behavior (e.g. asserting result.cipher/result.mac shapes and exact byte lengths on detached encryption). Return-value assertions in the generated wrappers (assert_retval in each symbol JSON) surface native libsodium failures as thrown JS errors rather than silently returning invalid data. Biome enforces consistent formatting and import order, and the wrapper/build scripts are themselves TypeScript, giving the code-generation layer type coverage even though the generated runtime output is plain JS.

What Makes It Unique Rather than hand-writing ~256 wrapper functions around a C API, the project generates them from declarative JSON symbol definitions that describe each function’s buffer semantics (fixed-length vs. unsized, computed output lengths, state-address handling for streaming APIs) — the same generation pass produces the runtime wrapper, the TypeScript definitions, and the API documentation from one source of truth, so the JS-facing API can never drift from what the underlying WASM module actually exposes. It also gets the full breadth of libsodium (AEAD ciphers, key exchange, secret streaming, password hashing, all in one library) into any JS runtime, browser included, via a WASM build with an automatic asm.js compatibility fallback, rather than relying on native Node.js addons that don’t work in the browser.

Used by 10 apps in this directory

TypeScript
97%
Other

Infisical

Security · Devops

29,145

The open-source platform for secrets, certificates, privileged access, and AI agent security — all in one self-hostable system.

View details
91
Repo Health
84
Technical
65
Dependency
Built with
TypeScript97%
Updated yesterday
TypeScript
65%
Apache 2.0

Laminar

AI Development · Monitoring

3,230

Open-source observability platform purpose-built for AI agents — trace, evaluate, debug, and monitor at scale with SQL access and real-time replay.

View details
85
Repo Health
76
Technical
69
Dependency
Built with
TypeScript65%
Rust33%
Updated yesterday
TypeScript
96%
AGPL 3.0

Libra AI

AI Development · No Code Platforms

1,680

Open-source AI-powered platform that generates and deploys full-stack web applications from natural language prompts, built natively for Cloudflare Workers.

View details
31
Repo Health
71
Technical
68
Dependency
Built with
TypeScript96%
Updated 11 months ago
TypeScript
85%
GPL 3.0

Notesnook

Note Taking · File Storage · Security

14,533

End-to-end encrypted, open-source note-taking where your data stays yours — even from the server.

View details
90
Repo Health
84
Technical
60
Dependency
Built with
TypeScript85%
JavaScript12%
Updated 2 days ago
TypeScript
53%
Other

Phase Console

Security · Devops

914

End-to-end encrypted secrets management for engineering teams — from local dev to Kubernetes production.

View details
83
Repo Health
73
Technical
66
Dependency
Built with
TypeScript53%
Python45%
Updated 2 days ago
Python
54%
Other

PostHog

Analytics · Monitoring · Developer Tools

39,612

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python54%
TypeScript36%
Updated today
TypeScript
92%
Other

Hexclave

Developer Tools · Authentication

6,854

The open-source user infrastructure platform — authentication, teams, payments, emails, analytics, and more on a single unified user model.

View details
87
Repo Health
80
Technical
66
Dependency
Built with
TypeScript92%
Updated 2 days ago
TypeScript
82%
AGPL 3.0

Standard Notes

Note Taking

6,621

End-to-end encrypted notes and files — private by design, built to last, and fully self-hostable.

View details
88
Repo Health
83
Technical
67
Dependency
Built with
TypeScript82%
JavaScript14%
Updated 3 days ago
Rust
31%
Other

Windmill

Automation · Developer Tools

17,793

Turn scripts into webhooks, workflows, and auto-generated UIs — the fastest self-hostable workflow engine, 13x faster than Airflow.

View details
91
Repo Health
82
Technical
65
Dependency
Built with
Rust31%
TypeScript23%
Svelte23%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search