The best JavaScript encryption library is the Web Crypto API for anything it supports, because it is built into browsers, hardware-accelerated and adds no bundle size. For ChaCha20-Poly1305, Argon2 or Ed25519, use libsodium.js or the audited noble libraries, and use Node's built-in crypto module on the server.
Key takeaways
- Use the native Web Crypto API first: it is free, audited by browser vendors and the fastest option for AES-GCM.
- libsodium.js and the noble libraries fill the gaps Web Crypto leaves, such as ChaCha20-Poly1305, Argon2 and Ed25519.
- Never roll your own crypto, never use Math.random for keys, and avoid crypto-js for new code.
JavaScript needs encryption for passwords, messages, files and tokens — but the wrong library, or the wrong mode, can leave data exposed even when it looks encrypted. The good news is that the best choices in 2026 are free, audited and well documented.
How did we choose the best JavaScript encryption libraries?
We ranked libraries on audit history, algorithm coverage, bundle size, maintenance, browser and Node.js support, and whether they choose safe defaults for you. A library only made the list if it is actively maintained and its cryptography is authenticated by default.
- Audits: independent security audits, not just popularity on npm.
- Safe defaults: authenticated encryption (AEAD) and random nonces, not unauthenticated modes.
- Coverage: AES-GCM, ChaCha20-Poly1305, Ed25519, X25519, Argon2 and modern key derivation.
- Size and speed: native code is fastest, WebAssembly is next, and pure JavaScript is smallest but slowest for bulk data.
- Maintenance: recent releases, a public repository, and a clear security policy.
Which JavaScript encryption library is best? Compared
Web Crypto is best for anything it supports. libsodium.js is best when you need the widest coverage in one library. The noble libraries are best when bundle size matters.
| Library | Best for | Free plan | Starting price | Key feature |
|---|---|---|---|---|
| Web Crypto API | Browser and Node, no bundle | Free, built in | Free | AES-GCM, hardware-accelerated |
| libsodium.js | Widest coverage in one library | Free, open source | Free | ChaCha20-Poly1305, Argon2id, Ed25519 |
| @noble/ciphers | Small, audited symmetric crypto | Free, open source | Free | AES, ChaCha20, Poly1305, audited by Cure53 |
| @noble/curves | Signatures and key exchange | Free, open source | Free | Ed25519, X25519, secp256k1 |
| @noble/hashes | Hashing and password hashing | Free, open source | Free | SHA-256, BLAKE3, Argon2id |
| tweetnacl | Tiny NaCl-compatible crypto | Free, open source | Free | Curve25519, Ed25519, secretbox |
| node:crypto | Server-side Node.js | Free, built in | Free | Full OpenSSL coverage, no install |
All seven are free and open source. The choice is about coverage, size and where the code runs, not price.
What should you look for in a JavaScript encryption library?
Look for authenticated encryption, safe defaults and an audit. Authenticated encryption — AES-GCM or ChaCha20-Poly1305 — both encrypts data and proves it has not been changed. Older modes such as AES-CBC without a separate authentication tag do not, and getting that wrong is a common source of real vulnerabilities.
Also check that the library generates nonces for you or makes it hard to reuse one, that it uses a secure random source, and that password hashing uses Argon2id or a similarly slow function. OWASP's cryptographic storage guidance makes the same point: use a vetted library, use authenticated encryption, and never design your own scheme. If you need to check a value quickly, our free hash generator shows common hashes without installing anything.
1. Web Crypto API — best for browser and Node, no bundle
The Web Crypto API is built into every modern browser and into Node.js. It adds zero bytes to your bundle, is hardware-accelerated where available, and is maintained by browser vendors. It is the baseline every other option is measured against.
- Algorithms: AES-GCM, RSA-OAEP, ECDH and ECDSA, PBKDF2 and HKDF, plus Ed25519 and X25519 in newer implementations.
- Missing: No ChaCha20-Poly1305 and no Argon2, by design, because its algorithm coverage is intentionally narrow.
- Keys: Supports non-extractable keys, so a key can be used but not read out of the browser.
Pros: no install, no bundle, fastest for AES-GCM, hardware-backed where available. Cons: narrow coverage, and the API is verbose. Pick it if: you only need AES-GCM, RSA or elliptic-curve key exchange, and you want the simplest, fastest path.
2. libsodium.js — best for widest coverage in one library
libsodium.js is the WebAssembly build of libsodium, a portable fork of Daniel J. Bernstein's NaCl library. It is the default pick when Web Crypto is not enough, because it covers almost everything in one audited package.
- Algorithms: XChaCha20-Poly1305, XSalsa20-Poly1305, AES-256-GCM, Ed25519, X25519, Argon2id, BLAKE2b, and a streaming API for encrypting files.
- Size: Larger than pure JavaScript libraries, at roughly 200 to 300 KB gzipped for the WebAssembly build, because it carries the whole library.
- Style: High-level operations that do one complete job, such as sealing a message or hashing a password, so it is hard to combine the pieces wrongly.
Pros: widest coverage, safe defaults, multiple audits, consistent API across browser and Node. Cons: large bundle, and WebAssembly adds build complexity. Pick it if: you need ChaCha20-Poly1305, Argon2 or streaming encryption, and bundle size is not your main constraint.
3. @noble/ciphers — best for small, audited symmetric crypto
@noble/ciphers is a pure TypeScript library with zero dependencies, audited by Cure53 in 2023. It is tree-shakeable, so you only ship the algorithms you import — about 8 KB gzipped for the cipher primitives alone, or about 3 KB for ChaCha20 alone.
- Algorithms: AES in GCM, GCM-SIV, CTR, CBC and other modes, ChaCha20 and XChaCha20, Poly1305, Salsa20 and XSalsa20.
- Design: Small, readable TypeScript, with cross-library tests to check correctness.
- Use: Import only what you need, for example the ChaCha20-Poly1305 function, rather than the whole package.
Pros: tiny, audited, zero dependencies, TypeScript-native. Cons: pure JavaScript is slower than Web Crypto or WebAssembly for bulk AES-GCM. Pick it if: bundle size matters and you need ChaCha20 or AES outside Web Crypto.
4. @noble/curves — best for signatures and key exchange
@noble/curves is the companion to @noble/ciphers for elliptic-curve cryptography. It is also pure TypeScript with zero dependencies, and has been audited by Trail of Bits and Cure53.
- Algorithms: Ed25519 signatures, X25519 key exchange, secp256k1, and BLS signatures.
- Size: About 35 KB gzipped for the curves package, tree-shakeable by curve.
- Use: Signing messages, verifying signatures, and agreeing shared keys without a server.
Pros: audited, small, no dependencies, works in browsers, Node and React Native with a polyfill for random values. Cons: it does not encrypt data on its own — pair it with @noble/ciphers or libsodium.js. Pick it if: you need Ed25519 or X25519 and you want a small, audited pure-JavaScript implementation.
5. @noble/hashes — best for hashing and password hashing
@noble/hashes is the hashing companion to the noble family: SHA-256, SHA-512, BLAKE2b, BLAKE3 and Argon2id in a single audited package. It is the right place to start when you need to hash data or store passwords.
- Password hashing: Argon2id is the modern choice for passwords, and it is included here.
- General hashing: SHA-256 and SHA-512 for checksums and integrity, BLAKE3 for speed.
- Size: Small and tree-shakeable, like the rest of the noble family.
Pros: audited, small, covers both fast hashes and slow password hashes. Cons: it is a hashing library, not a full encryption library. Pick it if: you need Argon2id or BLAKE3 in JavaScript without the full libsodium.js bundle.
6. tweetnacl — best for tiny NaCl-compatible crypto
tweetnacl is a JavaScript port of TweetNaCl, the compact C implementation of the NaCl library. At about 15 KB gzipped, it is the smallest option on this list that still offers modern, authenticated primitives.
- Algorithms: Curve25519 key exchange, Ed25519 signatures, and XSalsa20-Poly1305 secretbox and box encryption.
- Compatibility: Compatible with NaCl and libsodium for the primitives it implements.
- Limitation: No ChaCha20-Poly1305 and no Argon2, so it is narrower than libsodium.js.
Pros: tiny, simple, audited lineage through NaCl. Cons: narrower coverage and less active development than the noble libraries. Pick it if: you need a minimal NaCl-compatible library and you do not need ChaCha20 or Argon2.
7. node:crypto — best for server-side Node.js
On the server, Node.js ships its own crypto module, node:crypto, built on OpenSSL. It needs no install, offers comprehensive algorithm coverage, and is the right choice for server-side encryption, hashing and TLS work.
- Algorithms: AES-GCM and other AEAD modes, RSA, elliptic curves, PBKDF2, scrypt and Argon2 through OpenSSL, plus the Web Crypto API in Node.js itself.
- Modern use: Use the AEAD interfaces such as createCipheriv with an AEAD mode. The older createCipher interface was removed in Node.js 22.
- Randomness: crypto.randomBytes gives cryptographically secure random bytes on the server.
Pros: built in, comprehensive, maintained with Node.js and OpenSSL. Cons: server-only, so it does not help in the browser. Pick it if: you are writing server-side JavaScript and you do not want another dependency.
How do you choose the right JavaScript encryption library?
Start with the native API, then add one library for the gaps. That order keeps your bundle small and your code audited.
- Browser app, only AES-GCM or RSA? Use Web Crypto and nothing else.
- Need ChaCha20-Poly1305, Argon2 or streaming? Use libsodium.js if bundle size is fine, or @noble/ciphers plus @noble/hashes if it is not.
- Need signatures or key exchange? Use @noble/curves, or libsodium.js if you already ship it.
- Server only? Use node:crypto, or the Web Crypto API in Node.js for code shared with the browser.
- Small embedded or library code? Use tweetnacl or the noble libraries for the smallest footprint.
What should you avoid in JavaScript encryption?
Avoid crypto-js for new code: it is unmaintained, has no current audit, and defaults to older modes. Avoid unauthenticated encryption such as AES-CBC without a separate authentication tag, and avoid ECB mode entirely — it encrypts identical blocks identically, which leaks patterns.
Never use Math.random for keys, nonces or tokens. Use crypto.getRandomValues in the browser or crypto.randomBytes in Node.js. Never reuse a nonce with the same key, never hard-code a key in client-side code, and never invent your own cipher or protocol. If a library has no audit, no recent release and no clear maintenance, do not use it for anything that protects real data.
The bottom line
Use Web Crypto for anything it supports, add libsodium.js or the noble libraries for ChaCha20-Poly1305, Argon2 and Ed25519, and use node:crypto on the server. That combination covers almost every JavaScript encryption job with audited, maintained code.
Before you ship, check your hashes and encodings with the free hash generator, and keep your keys out of client-side code. For the official API details, see MDN's Web Crypto documentation and the libsodium documentation.
If you store passwords, review the pwned password checker to see whether a password already appears in known breaches.
Frequently asked questions
What is the best encryption library for JavaScript?
The Web Crypto API is the best starting point, because it is built into every modern browser and is hardware-accelerated. Add libsodium.js or the noble libraries only for algorithms Web Crypto does not offer.
Is Web Crypto API enough for most apps?
Yes, for most apps. It offers AES-GCM, RSA-OAEP, ECDH and ECDSA, PBKDF2 and HKDF. It does not offer ChaCha20-Poly1305, Argon2 or, in older browsers, Ed25519, so apps that need those use a library instead.
Is libsodium.js safe to use?
Yes. libsodium.js is the WebAssembly build of libsodium, a widely used C library that has been audited multiple times. It is the default choice when Web Crypto is not enough.
What is the difference between noble ciphers and noble curves?
The noble family is split by job: @noble/ciphers handles symmetric encryption such as AES and ChaCha20, @noble/curves handles elliptic curves such as Ed25519 and X25519, and @noble/hashes handles hashing and password hashing.
Should I use crypto-js in a new project?
No. crypto-js was popular for years, but it is unmaintained, has no current audit, and defaults to older modes such as AES-CBC without authentication. Use Web Crypto or a noble library instead.
How do I encrypt a password in JavaScript?
Do not encrypt passwords; hash them with a slow, memory-hard function such as Argon2id, using libsodium.js or @noble/hashes. On a server, use a dedicated password-hashing function, never a fast hash such as SHA-256 alone.
Can I use the same library in the browser and Node.js?
Yes. The Web Crypto API is available in both modern browsers and Node.js, and libsodium.js and the noble libraries run in both. Node also has its own built-in crypto module on top of OpenSSL.
Do I need a library for random numbers in JavaScript?
No extra library is needed for randomness: use crypto.getRandomValues in the browser or crypto.randomBytes in Node.js. Never use Math.random for keys, nonces or tokens, because it is not cryptographically secure.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against MDN Web Crypto documentation, libsodium documentation, the noble library package pages, Node.js crypto documentation and OWASP cryptographic guidance.
How we write and fact-check →