@@ -62,10 +62,10 @@ import { managedNonce, randomBytes } from '@noble/ciphers/webcrypto';
62
62
- [ Examples] ( #examples )
63
63
- [ XChaCha20-Poly1305 encryption] ( #xchacha20-poly1305-encryption )
64
64
- [ AES-256-GCM encryption] ( #aes-256-gcm-encryption )
65
+ - [ managedNonce: automatic nonce handling] ( #managednonce-automatic-nonce-handling )
65
66
- [ AES: gcm, siv, ctr, cfb, cbc, ecb] ( #aes-gcm-siv-ctr-cfb-cbc-ecb )
66
- - [ Friendly WebCrypto AES] ( #friendly-webcrypto-aes )
67
- - [ AESKW and AESKWP] ( #aeskw-and-aeskwp )
68
- - [ Auto-handle nonces] ( #auto-handle-nonces )
67
+ - [ AES: friendly WebCrypto wrapper] ( #aes-friendly-webcrypto-wrapper )
68
+ - [ AES: AESKW and AESKWP] ( #aeskw-and-aeskwp )
69
69
- [ Reuse array for input and output] ( #reuse-array-for-input-and-output )
70
70
- [ Internals] ( #internals )
71
71
- [ Implemented primitives] ( #implemented-primitives )
@@ -119,6 +119,29 @@ const ciphertext = aes.encrypt(data);
119
119
const data_ = aes .decrypt (ciphertext); // utils.bytesToUtf8(data_) === data
120
120
```
121
121
122
+ #### managedNonce: automatic nonce handling
123
+
124
+ We provide API that manages nonce internally instead of exposing them to library's user.
125
+
126
+ For ` encrypt ` , a ` nonceBytes ` -length buffer is fetched from CSPRNG and prenended to encrypted ciphertext.
127
+
128
+ For ` decrypt ` , first ` nonceBytes ` of ciphertext are treated as nonce.
129
+
130
+ > [ !WARN]
131
+ > AES-GCM & ChaCha (NOT xchacha) have 12-byte nonces, which limit amount of messages
132
+ > encryptable under the same key. Check out [ limits section] ( #encryption-limits ) .
133
+
134
+ ``` js
135
+ import { xchacha20poly1305 } from ' @noble/ciphers/chacha' ;
136
+ import { managedNonce } from ' @noble/ciphers/webcrypto' ;
137
+ import { hexToBytes , utf8ToBytes } from ' @noble/ciphers/utils' ;
138
+ const key = hexToBytes (' fa686bfdffd3758f6377abbc23bf3d9bdc1a0dda4a6e7f8dbdd579fa1ff6d7e1' );
139
+ const chacha = managedNonce (xchacha20poly1305)(key); // manages nonces for you
140
+ const data = utf8ToBytes (' hello, noble' );
141
+ const ciphertext = chacha .encrypt (data);
142
+ const data_ = chacha .decrypt (ciphertext);
143
+ ```
144
+
122
145
#### AES: gcm, siv, ctr, cfb, cbc, ecb
123
146
124
147
``` js
@@ -144,7 +167,7 @@ for (const cipher of [ecb]) {
144
167
}
145
168
```
146
169
147
- #### Friendly WebCrypto AES
170
+ #### AES: friendly WebCrypto wrapper
148
171
149
172
Noble implements AES. Sometimes people want to use built-in ` crypto.subtle ` instead. However, it has terrible API. We simplify access to built-ins.
150
173
@@ -167,7 +190,7 @@ for (const cipher of [ctr, cbc]) {
167
190
}
168
191
```
169
192
170
- #### AESKW and AESKWP
193
+ #### AES: AESKW and AESKWP
171
194
172
195
``` ts
173
196
import { aeskw , aeskwp } from ' @noble/ciphers/aes' ;
@@ -178,29 +201,6 @@ const keyData = hexToBytes('00112233445566778899AABBCCDDEEFF');
178
201
const ciphertext = aeskw (kek ).encrypt (keyData );
179
202
```
180
203
181
- #### Auto-handle nonces
182
-
183
- We provide API that manages nonce internally instead of exposing them to library's user.
184
-
185
- For ` encrypt ` , a ` nonceBytes ` -length buffer is fetched from CSPRNG and prenended to encrypted ciphertext.
186
-
187
- For ` decrypt ` , first ` nonceBytes ` of ciphertext are treated as nonce.
188
-
189
- > [ !WARN]
190
- > AES-GCM & ChaCha (NOT xchacha) have 12-byte nonces, which limit amount of messages
191
- > encryptable under the same key. Check out [ limits section] ( #encryption-limits ) .
192
-
193
- ``` js
194
- import { xchacha20poly1305 } from ' @noble/ciphers/chacha' ;
195
- import { managedNonce } from ' @noble/ciphers/webcrypto' ;
196
- import { hexToBytes , utf8ToBytes } from ' @noble/ciphers/utils' ;
197
- const key = hexToBytes (' fa686bfdffd3758f6377abbc23bf3d9bdc1a0dda4a6e7f8dbdd579fa1ff6d7e1' );
198
- const chacha = managedNonce (xchacha20poly1305)(key); // manages nonces for you
199
- const data = utf8ToBytes (' hello, noble' );
200
- const ciphertext = chacha .encrypt (data);
201
- const data_ = chacha .decrypt (ciphertext);
202
- ```
203
-
204
204
#### Reuse array for input and output
205
205
206
206
To avoid additional allocations, Uint8Array can be reused
0 commit comments