libsodium 0.7.15 → 0.8.0
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/LICENSE +1 -1
- package/README.md +71 -36
- package/dist/modules/libsodium.js +113 -1
- package/dist/modules-esm/libsodium.mjs +136 -0
- package/package.json +13 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
The [sodium](https://github.com/jedisct1/libsodium) crypto library
|
|
6
6
|
compiled to WebAssembly and pure JavaScript using
|
|
7
|
-
[Emscripten](https://github.com/
|
|
7
|
+
[Emscripten](https://github.com/emscripten-core/emscripten), with
|
|
8
8
|
automatically generated wrappers to make it easy to use in web
|
|
9
9
|
applications.
|
|
10
10
|
|
|
11
|
-
The complete library weighs
|
|
11
|
+
The complete library weighs about 310 KB (minified, gzipped, includes pure JS +
|
|
12
12
|
WebAssembly versions) and can run in a web browser as well as server-side.
|
|
13
13
|
|
|
14
14
|
### Compatibility
|
|
@@ -43,11 +43,15 @@ It contains code for commonly used functions.
|
|
|
43
43
|
is a superset of the previous script, that contains all functions,
|
|
44
44
|
including rarely used ones and undocumented ones.
|
|
45
45
|
- [modules](https://github.com/jedisct1/libsodium.js/tree/master/dist/modules)
|
|
46
|
-
includes commonly used functions, and is designed to be loaded as a module.
|
|
46
|
+
includes commonly used functions, and is designed to be loaded as a CommonJS module.
|
|
47
47
|
`libsodium-wrappers` is the module your application should load, which
|
|
48
48
|
will in turn automatically load `libsodium` as a dependency.
|
|
49
49
|
- [modules-sumo](https://github.com/jedisct1/libsodium.js/tree/master/dist/modules-sumo)
|
|
50
50
|
contains sumo variants of the previous modules.
|
|
51
|
+
- [modules-esm](https://github.com/jedisct1/libsodium.js/tree/master/dist/modules-esm)
|
|
52
|
+
contains ESM (ES modules) versions with `.mjs` extensions.
|
|
53
|
+
- [modules-sumo-esm](https://github.com/jedisct1/libsodium.js/tree/master/dist/modules-sumo-esm)
|
|
54
|
+
contains sumo ESM variants.
|
|
51
55
|
|
|
52
56
|
The modules are also available on npm:
|
|
53
57
|
- [libsodium-wrappers](https://www.npmjs.com/package/libsodium-wrappers)
|
|
@@ -56,44 +60,63 @@ The modules are also available on npm:
|
|
|
56
60
|
### Usage (as a module)
|
|
57
61
|
|
|
58
62
|
Load the `libsodium-wrappers` module. The returned object contains a `.ready`
|
|
59
|
-
property: a promise that must be
|
|
63
|
+
property: a promise that must be resolved before the sodium functions
|
|
60
64
|
can be used.
|
|
61
65
|
|
|
62
66
|
Example:
|
|
63
67
|
|
|
64
68
|
```js
|
|
65
|
-
import
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
69
|
+
import sodium from 'libsodium-wrappers';
|
|
70
|
+
|
|
71
|
+
await sodium.ready;
|
|
72
|
+
|
|
73
|
+
let key = sodium.crypto_secretstream_xchacha20poly1305_keygen();
|
|
74
|
+
|
|
75
|
+
let res = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);
|
|
76
|
+
let [state_out, header] = [res.state, res.header];
|
|
77
|
+
let c1 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out,
|
|
78
|
+
sodium.from_string('message 1'), null,
|
|
79
|
+
sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE);
|
|
80
|
+
let c2 = sodium.crypto_secretstream_xchacha20poly1305_push(state_out,
|
|
81
|
+
sodium.from_string('message 2'), null,
|
|
82
|
+
sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL);
|
|
83
|
+
|
|
84
|
+
let state_in = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, key);
|
|
85
|
+
let r1 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c1);
|
|
86
|
+
let [m1, tag1] = [sodium.to_string(r1.message), r1.tag];
|
|
87
|
+
let r2 = sodium.crypto_secretstream_xchacha20poly1305_pull(state_in, c2);
|
|
88
|
+
let [m2, tag2] = [sodium.to_string(r2.message), r2.tag];
|
|
89
|
+
|
|
90
|
+
console.log(m1);
|
|
91
|
+
console.log(m2);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**Named exports:** The ESM modules also provide named exports for helper functions:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { ready, from_hex, to_hex, from_string, to_string } from 'libsodium-wrappers';
|
|
98
|
+
|
|
99
|
+
await ready;
|
|
100
|
+
const bytes = from_hex('deadbeef');
|
|
101
|
+
console.log(to_hex(bytes));
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**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:
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
import sodium from 'libsodium-wrappers';
|
|
108
|
+
|
|
109
|
+
await sodium.ready;
|
|
110
|
+
// Now crypto functions and constants are available on the sodium object
|
|
111
|
+
const key = sodium.crypto_secretbox_keygen();
|
|
112
|
+
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
|
|
90
113
|
```
|
|
91
114
|
|
|
92
115
|
### Usage (in a web browser, via a callback)
|
|
93
116
|
|
|
94
117
|
The `sodium.js` file includes both the core libsodium functions, as
|
|
95
118
|
well as the higher-level JavaScript wrappers. It can be loaded
|
|
96
|
-
|
|
119
|
+
asynchronously.
|
|
97
120
|
|
|
98
121
|
A `sodium` object should be defined in the global namespace, with the
|
|
99
122
|
following property:
|
|
@@ -114,6 +137,14 @@ Example:
|
|
|
114
137
|
<script src="sodium.js" async></script>
|
|
115
138
|
```
|
|
116
139
|
|
|
140
|
+
**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:
|
|
141
|
+
|
|
142
|
+
```html
|
|
143
|
+
<meta charset="utf-8">
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
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.
|
|
147
|
+
|
|
117
148
|
## Additional helpers
|
|
118
149
|
|
|
119
150
|
* `from_base64()`, `to_base64()` with an optional second parameter
|
|
@@ -159,7 +190,11 @@ let key = sodium.from_hex('724b092810ec86d7e35c9d067702b31ef90bc43a7b59862674991
|
|
|
159
190
|
|
|
160
191
|
function encrypt_and_prepend_nonce(message) {
|
|
161
192
|
let nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
|
|
162
|
-
|
|
193
|
+
let ciphertext = sodium.crypto_secretbox_easy(message, nonce, key);
|
|
194
|
+
let result = new Uint8Array(nonce.length + ciphertext.length);
|
|
195
|
+
result.set(nonce);
|
|
196
|
+
result.set(ciphertext, nonce.length);
|
|
197
|
+
return result;
|
|
163
198
|
}
|
|
164
199
|
|
|
165
200
|
function decrypt_after_extracting_nonce(nonce_and_ciphertext) {
|
|
@@ -185,13 +220,13 @@ returns the following object:
|
|
|
185
220
|
|
|
186
221
|
### Standard vs Sumo version
|
|
187
222
|
|
|
188
|
-
The standard version (in the `dist/browsers
|
|
189
|
-
directories) contains the high-level functions, and
|
|
190
|
-
one for most projects.
|
|
223
|
+
The standard version (in the `dist/browsers`, `dist/modules`, and
|
|
224
|
+
`dist/modules-esm` directories) contains the high-level functions, and
|
|
225
|
+
is the recommended one for most projects.
|
|
191
226
|
|
|
192
227
|
Alternatively, the "sumo" version, available in the
|
|
193
|
-
`dist/browsers-sumo` and `dist/modules-sumo`
|
|
194
|
-
the symbols from the original library. This includes undocumented,
|
|
228
|
+
`dist/browsers-sumo`, `dist/modules-sumo`, and `dist/modules-sumo-esm`
|
|
229
|
+
directories contains all the symbols from the original library. This includes undocumented,
|
|
195
230
|
untested, deprecated, low-level and easy to misuse functions.
|
|
196
231
|
|
|
197
232
|
The `crypto_pwhash_*` function set is only included in the sumo version.
|