Skip to content

Commit

Permalink
Fix readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 28, 2024
1 parent 3571520 commit b172df5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,20 @@ import { randomBytes } from '@noble/ciphers/webcrypto';

const key = randomBytes(32);
const nonce = randomBytes(12);
const inputLength = 12;
const tagLength = 16;

const input = utf8ToBytes('hello, noble'); // length == 12
const inputLength = 12;

// plaintext + ciphertext + tag: 28 bytes
const buf = new Uint8Array(inputLength + inputLength + tagLength);
const _data = utf8ToBytes('hello, noble'); // length == 12
buf.set(_data, 0); // first inputLength bytes
const _start = buf.subarray(0, inputLength);
const _end = buf.subarray(inputLength);
buf.set(msg, 0); // first inputLength bytes
const _start = buf.subarray(0, inputLength); // 0..12
const _end = buf.subarray(inputLength); // 12..28

const chacha = chacha20poly1305(key, nonce);
chacha.encrypt(_start, _end);
chacha.decrypt(_end, _start); // _start now same as _data
chacha.decrypt(_end, _start); // _start now same as msg
```

#### All imports
Expand Down

0 comments on commit b172df5

Please sign in to comment.