# CryptoJS — JavaScript Library of Crypto Standards > CryptoJS provides a collection of standard cryptographic algorithms implemented in JavaScript, including AES, SHA-256, HMAC, PBKDF2, and more, for both browser and Node.js environments. ## Install Save as a script file and run: # CryptoJS — JavaScript Library of Crypto Standards ## Quick Use ```bash npm install crypto-js ``` ```javascript import CryptoJS from 'crypto-js'; // AES encryption const encrypted = CryptoJS.AES.encrypt('secret message', 'passphrase').toString(); const decrypted = CryptoJS.AES.decrypt(encrypted, 'passphrase').toString(CryptoJS.enc.Utf8); // SHA-256 hash const hash = CryptoJS.SHA256('hello world').toString(); // HMAC const hmac = CryptoJS.HmacSHA256('message', 'secret-key').toString(); ``` ## Introduction CryptoJS is a widely used JavaScript library that implements standard cryptographic algorithms. It works in both browser and server environments without native dependencies, making it a practical choice for hashing, encryption, and message authentication in web applications. ## What CryptoJS Does - Implements symmetric encryption algorithms including AES, DES, TripleDES, and Rabbit - Provides hash functions such as MD5, SHA-1, SHA-256, SHA-512, and SHA-3 - Supports HMAC message authentication with any of its hash algorithms - Includes key derivation functions like PBKDF2 and EvpKDF - Offers multiple encoding formats including Hex, Base64, Latin1, and Utf8 ## Architecture Overview CryptoJS is organized as a set of algorithm modules that share a common WordArray core. WordArray represents binary data as an array of 32-bit words, enabling efficient bitwise operations. Each cipher follows the CipherBase interface with encrypt and decrypt methods, and hashers implement the Hasher interface with update and finalize. The library uses a progressive hashing design, allowing data to be fed in chunks for streaming use cases. ## Self-Hosting & Configuration - Install from npm and import individual algorithms or the full bundle - Use tree-shakeable imports like crypto-js/sha256 to reduce bundle size - Configure cipher modes (CBC, CFB, OFB, CTR, ECB) and padding schemes as needed - Set initialization vectors and salts explicitly for production encryption - Works in browsers via bundlers and directly in Node.js without polyfills ## Key Features - Pure JavaScript with no native addon dependencies - Supports progressive hashing for processing large data in chunks - Provides configurable cipher modes, padding, and key sizes - Compatible with OpenSSL-formatted output for interoperability - Individual algorithm imports enable smaller bundle sizes ## Comparison with Similar Tools - **Web Crypto API** — Browser-native and faster, but async-only and limited algorithm support in older browsers - **Node.js crypto** — Built into Node with native performance; not available in browsers - **libsodium.js** — Modern high-level crypto API; better defaults but different algorithm set - **Stanford JS Crypto (sjcl)** — Smaller footprint but fewer algorithms and less active maintenance - **forge (node-forge)** — Broader scope including TLS and X.509; larger library for crypto-only needs ## FAQ **Q: Is CryptoJS suitable for production security?** A: For hashing and HMAC, yes. For encryption, prefer the Web Crypto API when available, as it runs in a secure context with constant-time operations. **Q: How do I reduce bundle size?** A: Import specific algorithms (e.g., import SHA256 from crypto-js/sha256) instead of the full library. **Q: Can CryptoJS decrypt data encrypted by OpenSSL?** A: Yes. CryptoJS supports OpenSSL-compatible key derivation and output formatting by default. **Q: Does CryptoJS support streaming or chunked hashing?** A: Yes. Use the progressive hasher pattern: create a hasher instance, call update() with chunks, and finalize() to get the digest. ## Sources - https://github.com/brix/crypto-js - https://cryptojs.gitbook.io/docs/ --- Source: https://tokrepo.com/en/workflows/asset-c212a26b Author: Script Depot