libsodium 0.7.16 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -29
- package/dist/modules/libsodium.js +1 -110
- package/dist/modules-esm/libsodium.mjs +1 -143
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,8 +8,9 @@ compiled to WebAssembly and pure JavaScript using
|
|
|
8
8
|
automatically generated wrappers to make it easy to use in web
|
|
9
9
|
applications.
|
|
10
10
|
|
|
11
|
-
The
|
|
12
|
-
WebAssembly versions)
|
|
11
|
+
The standard version weighs about 290 KB and the sumo version about 375 KB
|
|
12
|
+
(minified, gzipped, includes pure JS + WebAssembly versions). Both can run in
|
|
13
|
+
a web browser as well as server-side.
|
|
13
14
|
|
|
14
15
|
### Compatibility
|
|
15
16
|
|
|
@@ -60,37 +61,56 @@ The modules are also available on npm:
|
|
|
60
61
|
### Usage (as a module)
|
|
61
62
|
|
|
62
63
|
Load the `libsodium-wrappers` module. The returned object contains a `.ready`
|
|
63
|
-
property: a promise that must be
|
|
64
|
+
property: a promise that must be resolved before the sodium functions
|
|
64
65
|
can be used.
|
|
65
66
|
|
|
66
67
|
Example:
|
|
67
68
|
|
|
68
69
|
```js
|
|
69
|
-
import
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
70
|
+
import sodium from 'libsodium-wrappers';
|
|
71
|
+
|
|
72
|
+
await sodium.ready;
|
|
73
|
+
|
|
74
|
+
let key = sodium.crypto_secretstream_xchacha20poly1305_keygen();
|
|
75
|
+
|
|
76
|
+
let res = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);
|
|
77
|
+
let [state_out, header] = [res.state, res.header];
|
|
78
|
+
let c1 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out,
|
|
79
|
+
sodium.from_string('message 1'), null,
|
|
80
|
+
sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE);
|
|
81
|
+
let c2 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out,
|
|
82
|
+
sodium.from_string('message 2'), null,
|
|
83
|
+
sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL);
|
|
84
|
+
|
|
85
|
+
let state_in = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key);
|
|
86
|
+
let r1 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c1);
|
|
87
|
+
let [m1, tag1] = [sodium.to_string(r1.message), r1.tag];
|
|
88
|
+
let r2 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c2);
|
|
89
|
+
let [m2, tag2] = [sodium.to_string(r2.message), r2.tag];
|
|
90
|
+
|
|
91
|
+
console.log(m1);
|
|
92
|
+
console.log(m2);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Named exports:** The ESM modules also provide named exports for helper functions:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import { ready, from_hex, to_hex, from_string, to_string } from 'libsodium-wrappers';
|
|
99
|
+
|
|
100
|
+
await ready;
|
|
101
|
+
const bytes = from_hex('deadbeef');
|
|
102
|
+
console.log(to_hex(bytes));
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Note:** Cryptographic functions (like `crypto_secretbox_easy`) and constants (like `crypto_secretbox_KEYBYTES`) are dynamically added to the module at runtime after `ready` resolves. They cannot be imported as named exports and must be accessed via the default export:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
import sodium from 'libsodium-wrappers';
|
|
109
|
+
|
|
110
|
+
await sodium.ready;
|
|
111
|
+
// Now crypto functions and constants are available on the sodium object
|
|
112
|
+
const key = sodium.crypto_secretbox_keygen();
|
|
113
|
+
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
|
|
94
114
|
```
|
|
95
115
|
|
|
96
116
|
### Usage (in a web browser, via a callback)
|
|
@@ -118,6 +138,14 @@ Example:
|
|
|
118
138
|
<script src="sodium.js" async></script>
|
|
119
139
|
```
|
|
120
140
|
|
|
141
|
+
**Important:** If you inline the library directly into your HTML (rather than loading it from a separate file), make sure your page declares UTF-8 encoding:
|
|
142
|
+
|
|
143
|
+
```html
|
|
144
|
+
<meta charset="utf-8">
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Without this, browsers may corrupt the embedded WebAssembly binary data during HTML parsing, leading to errors like "failed to match magic number" or "HEAPU8 is undefined". Also ensure your server sends the `charset=utf-8` header.
|
|
148
|
+
|
|
121
149
|
## Additional helpers
|
|
122
150
|
|
|
123
151
|
* `from_base64()`, `to_base64()` with an optional second parameter
|
|
@@ -188,7 +216,7 @@ Functions returning more than one output buffer are returning them as
|
|
|
188
216
|
an object. For example, the `sodium.crypto_box_keypair()` function
|
|
189
217
|
returns the following object:
|
|
190
218
|
```javascript
|
|
191
|
-
{
|
|
219
|
+
{ publicKey: (Uint8Array), privateKey: (Uint8Array), keyType: 'x25519' }
|
|
192
220
|
```
|
|
193
221
|
|
|
194
222
|
### Standard vs Sumo version
|
|
@@ -223,6 +251,12 @@ Running `make` will install the dev dependencies, clone libsodium,
|
|
|
223
251
|
build it, test it, build the wrapper, and create the modules and
|
|
224
252
|
minified distribution files.
|
|
225
253
|
|
|
254
|
+
### Benchmarks
|
|
255
|
+
|
|
256
|
+
Run `make benchmark` to measure the performance of the cryptographic functions.
|
|
257
|
+
You can also run specific benchmarks with `bun benchmark/index.ts --only <name>`
|
|
258
|
+
(use `--list` to see available benchmarks).
|
|
259
|
+
|
|
226
260
|
## Related projects
|
|
227
261
|
|
|
228
262
|
* [react-native-libsodium](https://github.com/serenity-kit/react-native-libsodium): React Native bindings to Libsodium matching the libsodium-wrappers package API
|