### Password-Based Encryption with Scrypt Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Shows how to derive an AES key from a password using the scrypt KDF for secure encryption. It emphasizes the importance of using a unique salt and a high security level. Ensure '@noble/hashes' is installed. ```javascript import { xchacha20poly1305 } from '@noble/ciphers/chacha.js'; import { managedNonce } from '@noble/ciphers/utils.js'; import { scrypt } from '@noble/hashes/scrypt.js'; // Convert password into 32-byte key using scrypt const PASSWORD = 'correct-horse-battery-staple'; const APP_SPECIFIC_SECRET = 'salt-12345678-secret'; const SECURITY_LEVEL = 2 ** 20; // requires 1GB of RAM to calculate // sync, but scryptAsync is also available const key = scrypt(PASSWORD, APP_SPECIFIC_SECRET, { N: SECURITY_LEVEL, r: 8, p: 1, dkLen: 32, maxmem: 2 ** 30 + 4096, }); // Use random, managed nonce const chacha = managedNonce(xchacha20poly1305)(key); const data = new TextEncoder().encode('hello noble'); const ciphertext = chacha.encrypt(data); const data_ = chacha.decrypt(ciphertext); ``` -------------------------------- ### Run AES and SIV Benchmarks Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for AES and SIV (Synthetic Initialization Vector) modes. ```sh node aes.js ``` -------------------------------- ### Run Benchmarks Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Execute the benchmark suite to measure performance. Results are typically measured on modern hardware like Apple M4. ```sh npm run bench ``` -------------------------------- ### Run XChaCha-Poly Benchmarks (All Libraries) Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for XChaCha-Poly and other AEAD ciphers using all available cryptographic libraries. ```sh node aead.js ``` -------------------------------- ### aes-ctr-256 (encrypt, 1MB) Comparison Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Compares the performance of noble's aes-ctr-256 implementation (including Web Crypto wrapper) against stablelib, aesjs, and awasm-noble for encrypting 1MB of data. ```text aes-ctr-256 (encrypt, 1MB) ├─stablelib x 123 mb/sec ├─aesjs x 42 mb/sec ├─awasm-noble_thread x 2,105 mb/sec ├─awasm-noble_no_threads x 272 mb/sec ├─noble_webcrypto x 5,965 mb/sec └─noble x 124 mb/sec ``` -------------------------------- ### Demonstrate XORing Ciphertexts Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Illustrates how repeating a (key, nonce) pair with different plaintexts can lead to decryption by XORing the ciphertexts. This highlights the importance of unique nonces. ```plaintext ciphertext_a = encrypt(plaintext_a, key, nonce) ciphertext_b = encrypt(plaintext_b, key, nonce) stream_diff = xor(ciphertext_a, ciphertext_b) # Break encryption ``` -------------------------------- ### Run ChaCha Benchmarks (All Libraries) Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for ChaCha stream ciphers using all available cryptographic libraries. ```sh node ciphers.js ``` -------------------------------- ### Run XChaCha-Poly Benchmarks (Noble Library Only) Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for XChaCha-Poly and other AEAD ciphers using only the noble cryptographic library. ```sh node aead.js noble ``` -------------------------------- ### xsalsa20poly1305 (encrypt, 1MB) Comparison Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Compares the performance of noble's xsalsa20poly1305 implementation against tweetnacl and awasm-noble for encrypting 1MB of data. ```text xsalsa20poly1305 (encrypt, 1MB) ├─tweetnacl x 196 mb/sec ├─awasm-noble_threads x 2,318 mb/sec ├─awasm-noble_no_threads x 1,196 mb/sec └─noble x 305 mb/sec ``` -------------------------------- ### Run Poly1305 Benchmarks Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for the Poly1305 message authentication code (MAC). ```sh node poly.js ``` -------------------------------- ### Run ChaCha Benchmarks (Noble Library Only) Source: https://github.com/paulmillr/noble-ciphers/blob/main/test/benchmark/README.md Execute benchmarks for ChaCha stream ciphers using only the noble cryptographic library. ```sh node ciphers.js noble ``` -------------------------------- ### AES Key Wrap (AESKW, AESKWP) Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Encrypts and decrypts key data using AES Key Wrap (AESKW) and AES Key Wrap with Padding (AESKWP). Requires a Key Encryption Key (KEK) and the key data to be wrapped. ```javascript import { aeskw, aeskwp } from '@noble/ciphers/aes.js'; import { hexToBytes } from '@noble/ciphers/utils.js'; const kek = hexToBytes('000102030405060708090A0B0C0D0E0F'); const keyData = hexToBytes('00112233445566778899AABBCCDDEEFF'); const ciphertext = aeskw(kek).encrypt(keyData); ``` -------------------------------- ### 64B Data Encryption Performance Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Benchmark results for encrypting 64-byte data chunks. Shows operations per second and time per operation for various ciphers. ```text 64B xsalsa20poly1305 x 735,835 ops/sec @ 1μs/op chacha20poly1305 x 581,395 ops/sec @ 1μs/op xchacha20poly1305 x 468,384 ops/sec @ 2μs/op aes-256-gcm x 201,126 ops/sec @ 4μs/op aes-256-gcm-siv x 162,284 ops/sec @ 6μs/op # Unauthenticated encryption salsa20 x 1,655,629 ops/sec @ 604ns/op xsalsa20 x 1,400,560 ops/sec @ 714ns/op chacha20 x 1,996,007 ops/sec @ 501ns/op xchacha20 x 1,404,494 ops/sec @ 712ns/op chacha8 x 2,145,922 ops/sec @ 466ns/op chacha12 x 2,036,659 ops/sec @ 491ns/op aes-ecb-256 x 1,019,367 ops/sec @ 981ns/op aes-cbc-256 x 931,966 ops/sec @ 1μs/op aes-ctr-256 x 954,198 ops/sec @ 1μs/op ``` -------------------------------- ### Randomness Generation Methods Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Illustrates different methods for generating cryptographically secure pseudorandom numbers. It's recommended to use these primarily for non-production or test environments. The ChaCha-based CSPRNG lacks a formal specification, making it less secure. ```javascript import { randomBytes } from '@noble/ciphers/utils.js'; import { rngAesCtrDrbg256 } from '@noble/ciphers/aes.js'; import { rngChacha8, rngChacha20 } from '@noble/ciphers/chacha.js'; // 1. Best: WebCrypto const rnd1 = randomBytes(32); // 2. AES-CTR DRBG const seed2 = randomBytes(48); const rnd2 = rngAesCtrDrbg256(seed2).randomBytes(1024); // 3. ChaCha8 CSPRNG const seed3 = randomBytes(32); const rnd3 = rngChacha8(seed3).randomBytes(1024); ``` -------------------------------- ### Nonce Generation with Counters Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Shows a method for ensuring unique nonces by using a sequential counter. This approach is suitable when a persistent counter can be maintained. ```plaintext for i in 0..: ciphertext[i] = encrypt(plaintexts[i], key, i) ``` -------------------------------- ### Nonce Generation with Random Values Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Demonstrates generating unique nonces by using a cryptographically secure random number generator for each encryption. This method is convenient but requires careful consideration of nonce length to avoid collisions. ```plaintext for i in 0..: rand_nonces[i] = random() ciphertext[i] = encrypt(plaintexts[i], key, rand_nonces[i]) ``` -------------------------------- ### 1MB Data Encryption Performance Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Benchmark results for encrypting 1MB data chunks. Shows operations per second and time per operation for various ciphers. ```text 1MB xsalsa20poly1305 x 334 ops/sec @ 2ms/op chacha20poly1305 x 333 ops/sec @ 2ms/op xchacha20poly1305 x 334 ops/sec @ 2ms/op aes-256-gcm x 94 ops/sec @ 10ms/op aes-256-gcm-siv x 90 ops/sec @ 11ms/op # Unauthenticated encryption salsa20 x 831 ops/sec @ 1ms/op xsalsa20 x 830 ops/sec @ 1ms/op chacha20 x 804 ops/sec @ 1ms/op xchacha20 x 797 ops/sec @ 1ms/op chacha8 x 1,495 ops/sec @ 668μs/op chacha12 x 1,148 ops/sec @ 871μs/op aes-ecb-256 x 289 ops/sec @ 3ms/op aes-cbc-256 x 114 ops/sec @ 8ms/op aes-ctr-256 x 127 ops/sec @ 7ms/op # Wrapper over built-in webcrypto webcrypto ctr-256 x 6,508 ops/sec @ 153μs/op webcrypto cbc-256 x 1,820 ops/sec @ 549μs/op webcrypto gcm-256 x 5,106 ops/sec @ 195μs/op ``` -------------------------------- ### Various AES Modes Encryption Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Demonstrates encryption and decryption for multiple AES modes including GCM, GCM-SIV, SIV, CTR, CFB, CBC, and ECB. Note the different nonce sizes for different modes. ```javascript import { gcm, gcmsiv, aessiv, ctr, cfb, cbc, ecb } from '@noble/ciphers/aes.js'; import { randomBytes } from '@noble/ciphers/utils.js'; const plaintext = new Uint8Array(32).fill(16); for (let cipher of [gcm, gcmsiv, aessiv]) { const key = randomBytes(32); // 24 for AES-192, 16 for AES-128 const nonce = randomBytes(12); const ciphertext_ = cipher(key, nonce).encrypt(plaintext); const plaintext_ = cipher(key, nonce).decrypt(ciphertext_); } for (const cipher of [ctr, cbc, cfb]) { const key = randomBytes(32); // 24 for AES-192, 16 for AES-128 const nonce = randomBytes(16); const ciphertext_ = cipher(key, nonce).encrypt(plaintext); const plaintext_ = cipher(key, nonce).decrypt(ciphertext_); } for (const cipher of [ecb]) { const key = randomBytes(32); // 24 for AES-192, 16 for AES-128 const ciphertext_ = cipher(key).encrypt(plaintext); const plaintext_ = cipher(key).decrypt(ciphertext_); } ``` -------------------------------- ### Reuse Array for Encryption and Decryption Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Demonstrates how to reuse a Uint8Array for both encryption and decryption to avoid extra memory allocations. Note that some ciphers may not support unaligned destination arrays efficiently. ```javascript import { chacha20poly1305 } from '@noble/ciphers/chacha.js'; import { randomBytes } from '@noble/ciphers/utils.js'; const key = randomBytes(32); const nonce = randomBytes(12); const chacha = chacha20poly1305(key, nonce); const input = new TextEncoder().encode('hello noble'); // length == 12 const inputLength = input.length; const tagLength = 16; const buf = new Uint8Array(inputLength + tagLength); const start = buf.subarray(0, inputLength); start.set(input); // copy input to buf chacha.encrypt(start, buf); // encrypt into `buf` chacha.decrypt(buf, start); // decrypt into `start` ``` -------------------------------- ### Importing Ciphers from noble-ciphers Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Import specific cipher functions and utility modules from the noble-ciphers library. Use sub-imports to ensure small application size. Ensure you have the necessary polyfills for environments like React Native. ```typescript import { gcm, gcmsiv } from '@noble/ciphers/aes.js'; import { chacha20poly1305, xchacha20poly1305 } from '@noble/ciphers/chacha.js'; import { xsalsa20poly1305 } from '@noble/ciphers/salsa.js'; // Unauthenticated encryption: make sure to use HMAC or similar import { ctr, cfb, cbc, ecb } from '@noble/ciphers/aes.js'; import { salsa20, xsalsa20 } from '@noble/ciphers/salsa.js'; import { chacha20, xchacha20, chacha8, chacha12 } from '@noble/ciphers/chacha.js'; import { aeskw, aeskwp } from '@noble/ciphers/aes.js'; // KW import { bytesToHex, hexToBytes, managedNonce, randomBytes } from '@noble/ciphers/utils.js'; ``` -------------------------------- ### AES GCM Encryption with WebCrypto Wrapper Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Encrypts and decrypts data using AES-GCM via a simplified WebCrypto wrapper. These operations are asynchronous. ```javascript import { gcm, ctr, cbc } from '@noble/ciphers/webcrypto.js'; import { randomBytes } from '@noble/ciphers/utils.js'; const plaintext = new Uint8Array(32).fill(16); const key = randomBytes(32); for (const cipher of [gcm]) { const nonce = randomBytes(12); const ciphertext_ = await cipher(key, nonce).encrypt(plaintext); const plaintext_ = await cipher(key, nonce).decrypt(ciphertext_); } for (const cipher of [ctr, cbc]) { const nonce = randomBytes(16); const ciphertext_ = await cipher(key, nonce).encrypt(plaintext); const plaintext_ = await cipher(key, nonce).decrypt(ciphertext_); } ``` -------------------------------- ### AES-256-GCM Encryption Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Encrypts and decrypts data using AES-256-GCM. Requires a 32-byte key and a 24-byte nonce. ```javascript import { gcm } from '@noble/ciphers/aes.js'; import { randomBytes } from '@noble/ciphers/utils.js'; const key = randomBytes(32); const nonce = randomBytes(24); const data = new TextEncoder().encode('hello noble'); const aes = gcm(key, nonce); const ciphertext = aes.encrypt(data); const data_ = aes.decrypt(ciphertext); // new TextDecoder().decode(data_) === data ``` -------------------------------- ### Managed Nonce Encryption with XChaCha20-Poly1305 Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Encrypts and decrypts data using XChaCha20-Poly1305 with automatic nonce management. The nonce is prepended to the ciphertext during encryption and extracted from the ciphertext during decryption. ```javascript import { xchacha20poly1305 } from '@noble/ciphers/chacha.js'; import { hexToBytes, managedNonce } from '@noble/ciphers/utils.js'; const key = hexToBytes('fa686bfdffd3758f6377abbc23bf3d9bdc1a0dda4a6e7f8dbdd579fa1ff6d7e1'); const chacha = managedNonce(xchacha20poly1305)(key); // manages nonces for you const data = new TextEncoder().encode('hello noble'); const ciphertext = chacha.encrypt(data); const data_ = chacha.decrypt(ciphertext); ``` -------------------------------- ### XChaCha20-Poly1305 Encryption Source: https://github.com/paulmillr/noble-ciphers/blob/main/README.md Encrypts and decrypts data using XChaCha20-Poly1305. Ensure a unique nonce is used for each encryption operation with the same key. ```javascript import { xchacha20poly1305 } from '@noble/ciphers/chacha.js'; import { randomBytes } from '@noble/ciphers/utils.js'; const key = randomBytes(32); // random key // const key = new Uint8Array([ // existing key // 169, 88, 160, 139, 168, 29, 147, 196, 14, 88, 237, 76, 243, 177, 109, 140, // 195, 140, 80, 10, 216, 134, 215, 71, 191, 48, 20, 104, 189, 37, 38, 55, // ]); // import { hexToBytes } from '@noble/ciphers/utils.js'; // hex key // const key = hexToBytes('4b7f89bac90a1086fef73f5da2cbe93b2fae9dfbf7678ae1f3e75fd118ddf999'); const nonce = randomBytes(24); const chacha = xchacha20poly1305(key, nonce); const data = new TextEncoder().encode('hello noble'); const ciphertext = chacha.encrypt(data); const data_ = chacha.decrypt(ciphertext); // new TextDecoder().decode(data_) === data ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.