Skip to content

Commit 1cf1ed8

Browse files
committed
readme
1 parent 6bb966c commit 1cf1ed8

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

README.md

+28-28
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ import { managedNonce, randomBytes } from '@noble/ciphers/webcrypto';
6262
- [Examples](#examples)
6363
- [XChaCha20-Poly1305 encryption](#xchacha20-poly1305-encryption)
6464
- [AES-256-GCM encryption](#aes-256-gcm-encryption)
65+
- [managedNonce: automatic nonce handling](#managednonce-automatic-nonce-handling)
6566
- [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)
6969
- [Reuse array for input and output](#reuse-array-for-input-and-output)
7070
- [Internals](#internals)
7171
- [Implemented primitives](#implemented-primitives)
@@ -119,6 +119,29 @@ const ciphertext = aes.encrypt(data);
119119
const data_ = aes.decrypt(ciphertext); // utils.bytesToUtf8(data_) === data
120120
```
121121

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+
122145
#### AES: gcm, siv, ctr, cfb, cbc, ecb
123146

124147
```js
@@ -144,7 +167,7 @@ for (const cipher of [ecb]) {
144167
}
145168
```
146169

147-
#### Friendly WebCrypto AES
170+
#### AES: friendly WebCrypto wrapper
148171

149172
Noble implements AES. Sometimes people want to use built-in `crypto.subtle` instead. However, it has terrible API. We simplify access to built-ins.
150173

@@ -167,7 +190,7 @@ for (const cipher of [ctr, cbc]) {
167190
}
168191
```
169192

170-
#### AESKW and AESKWP
193+
#### AES: AESKW and AESKWP
171194

172195
```ts
173196
import { aeskw, aeskwp } from '@noble/ciphers/aes';
@@ -178,29 +201,6 @@ const keyData = hexToBytes('00112233445566778899AABBCCDDEEFF');
178201
const ciphertext = aeskw(kek).encrypt(keyData);
179202
```
180203

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-
204204
#### Reuse array for input and output
205205

206206
To avoid additional allocations, Uint8Array can be reused

0 commit comments

Comments
 (0)