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.
npm install sodium-javascriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic authenticated encryption and decryption using the `crypto_secretbox_easy` and `crypto_secretbox_open_easy` functions, including key and nonce generation.
Migrate to actively maintained alternatives like `libsodium-wrappers` (for Node.js and browser with WebAssembly) or `sodium-native` (for Node.js with native bindings) for production use.
Always check for function availability before relying on it. For full libsodium feature parity, consider `libsodium-wrappers` or `sodium-native`.
Transition to actively maintained cryptographic libraries that receive regular security audits and updates.
Refer strictly to the `sodium-native` documentation for correct buffer sizes and usage patterns. Consider using `sodium-universal` for a more ergonomic API or `libsodium-wrappers` which often handles buffer management automatically.
For performance-critical applications in Node.js, `sodium-native` is the preferred choice. For cross-platform (browser/Node.js) applications needing better performance, `libsodium-wrappers` offers WebAssembly execution.
Verify the function exists in the `sodium-javascript` API by checking its source or `sodium-native` documentation (which it aims to mimic). If not implemented, you must use an alternative library or find another way to achieve the cryptographic goal.
Ensure your browser environment has a `Buffer` polyfill (e.g., `buffer` npm package) if using a bundler like Webpack, or prefer `libsodium-wrappers` for browser-native execution.
Double-check that the `cipher`, `nonce`, and `key` buffers are exactly the same as those used for encryption. Ensure no data corruption occurred during storage or transmission. Also verify buffer lengths and types.
In Node.js ESM files, use `const sodium = require('sodium-javascript')` or dynamic import `const sodium = await import('sodium-javascript')`. In bundlers, ensure your configuration correctly handles CJS module imports into an ESM project.No dependency data recorded yet.