Installations
npm install libsodium-js-universal
Developer Guide
Typescript
No
Module System
CommonJS, UMD
Node Version
9.3.0
NPM Version
5.6.0
Score
73.2
Supply Chain
98.2
Quality
75.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
HTML (77.26%)
JavaScript (16.01%)
TypeScript (5.15%)
Makefile (1.47%)
Shell (0.12%)
Developer
jedisct1
Download Statistics
Total Downloads
1,009
Last Day
2
Last Week
7
Last Month
10
Last Year
85
GitHub Statistics
1,008 Stars
895 Commits
144 Forks
32 Watching
4 Branches
28 Contributors
Bundle Size
1.05 MB
Minified
387.64 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.7.4
Package Id
libsodium-js-universal@0.7.4
Size
200.73 kB
NPM Version
5.6.0
Node Version
9.3.0
Total Downloads
Cumulative downloads
Total Downloads
1,009
Last day
0%
2
Compared to previous day
Last week
133.3%
7
Compared to previous week
Last month
150%
10
Compared to previous month
Last year
-9.6%
85
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
libsodium.js
Overview
The sodium crypto library compiled to WebAssembly and pure Javascript using Emscripten, with automatically generated wrappers to make it easy to use in web applications.
The complete library weights 188 Kb (minified, gzipped, includes pure js + webassembly versions) and can run in a web browser as well as server-side.
Compatibility
Supported browsers/JS engines:
- Chrome >= 16
- Edge >= 0.11
- Firefox >= 21
- Mobile Safari on iOS >= 8.0 (older versions produce incorrect results)
- NodeJS
- Opera >= 15
- Safari >= 6 (older versions produce incorrect results)
Installation
The dist directory contains pre-built scripts. Copy the files from one of its subdirectories to your application:
- browsers includes a single-file script that can be included in web pages. It contains code for commonly used functions.
- browsers-sumo is a superset of the previous script, that contains all functions, including rarely used ones and undocumented ones.
- modules
includes commonly used functions, and is designed to be loaded as a module.
libsodium-wrappers
is the module your application should load, which will in turn automatically loadlibsodium
as a dependency. - modules-sumo contains sumo variants of the previous modules.
The modules are also available on npm:
If you prefer Bower:
1bower install libsodium.js
Usage (as a module)
Load the sodium-wrappers
module. The returned object contains a .ready
property: a promise that must be resolve before the sodium functions
can be used.
Example:
1const _sodium = require('libsodium-wrappers'); 2(async() => { 3 await _sodium.ready; 4 const sodium = _sodium; 5 6 let key = sodium.crypto_secretstream_xchacha20poly1305_keygen(); 7 8 let res = sodium.crypto_secretstream_xchacha20poly1305_init_push(key); 9 let [state_out, header] = [res.state, res.header]; 10 let c1 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out, 11 sodium.from_string('message 1'), null, 12 sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE); 13 let c2 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out, 14 sodium.from_string('message 2'), null, 15 sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL); 16 17 let state_in = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key); 18 let r1 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c1); 19 let [m1, tag1] = [sodium.to_string(r1.message), r1.tag]; 20 let r2 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c2); 21 let [m2, tag2] = [sodium.to_string(r2.message), r2.tag]; 22 23 console.log(m1); 24 console.log(m2); 25})();
Usage (in a web browser, via a callback)
The sodium.js
file includes both the core libsodium functions, as
well as the higher-level Javascript wrappers. It can be loaded
asynchronusly.
A sodium
object should be defined in the global namespace, with the
following property:
onload
: the function to call after the wrapper is initialized.
Example:
1<script> 2 window.sodium = { 3 onload: function (sodium) { 4 let h = sodium.crypto_generichash(64, sodium.from_string('test')); 5 console.log(sodium.to_hex(h)); 6 } 7 }; 8</script> 9<script src="sodium.js" async></script>
Additional helpers
from_base64()
,to_base64()
with an optional second parameter whose value is one of:base64_variants.ORIGINAL
,base64_variants.ORIGINAL_NO_PADDING
,base64_variants.URLSAFE
ors.base64_variants.URLSAFE_NO_PADDING
. Default isbase64_variants.URLSAFE_NO_PADDING
.from_hex()
,to_hex()
from_string()
,to_string()
pad(<buffer>, <block size>)
,unpad(<buffer>, <block size>)
memcmp()
(constant-time check for equality, returnstrue
orfalse
)compare() (constant-time comparison. Values must have the same size. Returns
-1,
0or
1`)memzero()
(applies toUint8Array
objects)increment()
(increments an arbitrary-long number stored as a little-endianUint8Array
- typically to increment nonces)add()
(adds two arbitrary-long numbers stored as little-endianUint8Array
vectors)is_zero()
(constant-time, checksUint8Array
objects for all zeros)
API
The API exposed by the wrappers is identical to the one of the C library, except that buffer lengths never need to be explicitly given.
Binary input buffers should be Uint8Array
objects. However, if a string
is given instead, the wrappers will automatically convert the string
to an array containing a UTF-8 representation of the string.
Example:
1var key = sodium.randombytes_buf(sodium.crypto_shorthash_KEYBYTES), 2 hash1 = sodium.crypto_shorthash(new Uint8Array([1, 2, 3, 4]), key), 3 hash2 = sodium.crypto_shorthash('test', key);
If the output is a unique binary buffer, it is returned as a
Uint8Array
object.
Example (secretbox):
1let key = sodium.from_hex('724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed'); 2 3function encrypt_and_prepend_nonce(message) { 4 let nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES); 5 return nonce.concat(sodium.crypto_secretbox_easy(message, nonce, key)); 6} 7 8function decrypt_after_extracting_nonce(nonce_and_ciphertext) { 9 if (nonce_and_ciphertext.length < sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES) { 10 throw "Short message"; 11 } 12 let nonce = nonce_and_ciphertext.slice(0, sodium.crypto_secretbox_NONCEBYTES), 13 ciphertext = nonce_and_ciphertext.slice(sodium.crypto_secretbox_NONCEBYTES); 14 return sodium.crypto_secretbox_open_easy(ciphertext, nonce, key); 15}
In addition, the from_hex
, to_hex
, from_string
, and to_string
functions are available to explicitly convert hexadecimal, and
arbitrary string representations from/to Uint8Array
objects.
Functions returning more than one output buffer are returning them as
an object. For example, the sodium.crypto_box_keypair()
function
returns the following object:
1{ keyType: 'curve25519', privateKey: (Uint8Array), publicKey: (Uint8Array) }
Standard vs Sumo version
The standard version (in the dist/browsers
and dist/modules
directories) contains the high-level functions, and is the recommended
one for most projects.
Alternatively, the "sumo" version, available in the
dist/browsers-sumo
and dist/modules-sumo
directories contains all
the symbols from the original library. This includes undocumented,
untested, deprecated, low-level and easy to misuse functions.
The crypto_pwhash_*
function set is also only included in the Sumo
version. The high amount of heap memory (allocated after loading)
required by these functions may not be desirable when they are not
being used.
The sumo version is slightly larger than the standard version, and should be used only if you really need the extra symbols it provides.
Compilation
If you want to compile the files yourself, the following dependencies need to be installed on your system:
- emscripten
- binaryen
- git
- nodejs
- make
- uglify-es (
yarn global add uglify-es
)
Running make
will clone libsodium, build it, test it, build the
wrapper, and create the modules and minified distribution files.
Authors
Built by Ahmad Ben Mrad, Frank Denis and Ryan Lester.
License
This wrapper is distributed under the ISC License.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/issues.yml:1
- Info: no jobLevel write permissions found
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
3 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/issues.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/jedisct1/libsodium.js/issues.yml/master?enable=pin
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
4.6
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More