API Reference

libsodium-wrappers

WebAssembly build of libsodium exposing modern authenticated encryption, hashing, and random byte helpers in JS.

Official Documentation ↗
CompatibilityThis documentation covers version v0.7. Check the official docs for the latest API surface.

Installation

npmnpm install libsodium-wrappers
pnpmpnpm add libsodium-wrappers

Methods

Overview

libsodium-wrappers is a WebAssembly build of libsodium providing authenticated encryption, hashing, and secure random helpers in JavaScript. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • sodium.ready — Resolves once the WebAssembly module has initialized; await it before any other call.
  • crypto_secretbox_easy — Encrypts and authenticates a message with a symmetric key and unique nonce.
  • randombytes_buf — Returns cryptographically secure random bytes of the requested length.

Example

const sodium = require('libsodium-wrappers');

await sodium.ready;
console.log(sodium.crypto_secretbox_KEYBYTES);

Notes

The module loads asynchronously, so await sodium.ready first. Nonces must never be reused with a key, and secret comparison should be constant time.

Verification

Encrypt a message and decrypt it back with the same key and nonce, confirming the plaintext matches the original.

FAQ

Why must I await sodium.ready?
The library loads a WebAssembly module asynchronously; calls before ready resolves will fail or be undefined.
Can a nonce be reused with the same key?
Never. Reusing a nonce with the same key breaks the security of the secretbox construction; generate a fresh random nonce per message.
How do I compare secrets safely?
Use sodium.memcmp rather than === so comparison time does not leak information about the values.