xsalsa20-csprng
CSPRNG and crypto library powered by XSalsa20. Small, fast, supports both browsers and Node.js, and optimized for CSPRNG usage.
yarn add xsalsa20-csprng
This library is optimized to be used as CSPRNG rather than encrypting large data. Thus, this library doesn't contain native code or webassembly binding to reduce FFI overhead.
Usage
class XSalsa20CSPRNG
See reference for the further details.
const rng = // Random 32bit int (-2147483648 ≤ x < 2147483648), takes about 85ns per each callconst i = rng// Random 32bit unsigned int (0 ≤ x < 4294967296)const u = rng// Random int of desired range (0 ≤ x < 10)const n = rng // You can use fixed nonce and keyconst nonce = /* nonce with 24 bytes */const key = /* key with 32 bytes */const deterministic = XSalsa20CSPRNG// Fixed nonce and key will result in deterministic outputconsoleconsoleconsole
class XSalsa20
See reference for the further details.
const nonce = /* nonce with 24 bytes */const key = /* key with 32 bytes */ // Encryptconst encoder = nonce keyconst plaintext = Uint8Arrayconst ciphertext = encoder // Decrypt : same with encryptionconst decoder = nonce keyconst decrypted = decoder// plaintext and decrypted have same contents // You can retrieve XSalsa20 streams if you wantconst gen = nonce keyconsoleconsole