Sodium JavaScript Cryptography Library

0.8.0 · abandoned · verified Sun Apr 19

sodium-javascript is a pure JavaScript implementation of the libsodium cryptographic library's API, leveraging `tweetnacl` as its foundation. It was developed to provide a browser-compatible alternative to `sodium-native`, which is a Node.js native addon binding to the C libsodium library. The package's current stable version is 0.8.0, but it explicitly states 'WIP - Work In Progress' in its README and was last published over four years ago (January 2022). This suggests the project is largely unmaintained or abandoned. Its key differentiator was offering a pure JavaScript fallback for environments where native bindings are not feasible (e.g., browsers). For modern cross-platform libsodium usage, alternatives like `libsodium-wrappers` (which utilizes WebAssembly for better performance and broader API coverage) are generally recommended.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic authenticated encryption and decryption using the `crypto_secretbox_easy` and `crypto_secretbox_open_easy` functions, including key and nonce generation.

const sodium = require('sodium-javascript')

const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)

sodium.randombytes_buf(key)
sodium.randombytes_buf(nonce)

const message = Buffer.from('Hello, World!')
const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)

sodium.crypto_secretbox_easy(cipher, message, nonce, key)

console.log('Encrypted:', cipher.toString('hex'))

const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)

sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)

console.log('Plaintext:', plainText.toString())

view raw JSON →