CryptoJS is a comprehensive JavaScript cryptography library implementing standard cryptographic algorithms. It provides a modular architecture with support for hash functions, encryption algorithms, HMAC authentication, key derivation functions, and various encoding utilities for secure applications.
npm install crypto-jsFull Library Import:
var CryptoJS = require("crypto-js");
// Access all algorithms: CryptoJS.AES, CryptoJS.SHA256, etc.ES6 Modular Import:
import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';CommonJS Modular Import:
var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
var Base64 = require("crypto-js/enc-base64");var CryptoJS = require("crypto-js");
// Hash a message
var hash = CryptoJS.SHA256("Hello World");
console.log(hash.toString()); // hex string
// Encrypt a message
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');
console.log(ciphertext.toString()); // base64 string
// Decrypt the message
var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);
console.log(originalText); // 'my message'
// Create HMAC
var hmac = CryptoJS.HmacSHA256("message", "key");
console.log(hmac.toString());CryptoJS is built around several key architectural components:
CryptoJS.lib) providing WordArray, Hasher, and Cipher templatesCryptoJS.enc) for format conversion between different representations_createHelper() and _createHmacHelper() patternsCryptographic hash algorithms for data integrity and digital signatures. All hash functions accept string or WordArray input and return WordArray output.
// Hash Functions
CryptoJS.MD5(message) // MD5 hash algorithm
CryptoJS.SHA1(message) // SHA-1 hash algorithm
CryptoJS.SHA224(message) // SHA-224 hash algorithm
CryptoJS.SHA256(message) // SHA-256 hash algorithm
CryptoJS.SHA384(message) // SHA-384 hash algorithm
CryptoJS.SHA512(message) // SHA-512 hash algorithm
CryptoJS.SHA3(message, cfg) // SHA-3 (Keccak) hash algorithm
CryptoJS.RIPEMD160(message) // RIPEMD-160 hash algorithmSymmetric encryption algorithms for data confidentiality with support for various cipher modes and padding schemes.
// Encryption Functions
CryptoJS.AES.encrypt(message, key, cfg) // AES encryption
CryptoJS.AES.decrypt(ciphertext, key, cfg) // AES decryption
CryptoJS.DES.encrypt(message, key, cfg) // DES encryption
CryptoJS.TripleDES.encrypt(message, key, cfg) // Triple DES encryption
CryptoJS.RC4.encrypt(message, key, cfg) // RC4 stream cipher
CryptoJS.Rabbit.encrypt(message, key, cfg) // Rabbit stream cipher
CryptoJS.Blowfish.encrypt(message, key, cfg) // Blowfish encryptionHash-based Message Authentication Code functions for message authentication and integrity verification.
// HMAC Functions
CryptoJS.HmacMD5(message, key) // HMAC using MD5
CryptoJS.HmacSHA1(message, key) // HMAC using SHA-1
CryptoJS.HmacSHA224(message, key) // HMAC using SHA-224
CryptoJS.HmacSHA256(message, key) // HMAC using SHA-256
CryptoJS.HmacSHA384(message, key) // HMAC using SHA-384
CryptoJS.HmacSHA512(message, key) // HMAC using SHA-512
CryptoJS.HmacSHA3(message, key) // HMAC using SHA-3
CryptoJS.HmacRIPEMD160(message, key) // HMAC using RIPEMD-160Password-based key derivation for generating cryptographic keys from passwords or passphrases.
// Key Derivation Functions
CryptoJS.PBKDF2(password, salt, cfg) // Password-Based Key Derivation Function 2
CryptoJS.EvpKDF(password, salt, cfg) // OpenSSL EVP Key Derivation FunctionFormat conversion utilities for transforming between different data representations used in cryptographic operations.
// Encoding Objects (with stringify/parse methods)
CryptoJS.enc.Hex // Hexadecimal encoding
CryptoJS.enc.Base64 // Base64 encoding
CryptoJS.enc.Base64url // Base64 URL-safe encoding
CryptoJS.enc.Utf8 // UTF-8 encoding
CryptoJS.enc.Utf16 // UTF-16 Big Endian encoding
CryptoJS.enc.Utf16BE // UTF-16 Big Endian encoding (alias)
CryptoJS.enc.Utf16LE // UTF-16 Little Endian encoding
CryptoJS.enc.Latin1 // Latin1 encodingCipher modes, padding schemes, and formatters for customizing encryption behavior and output formats.
// Cipher Modes
CryptoJS.mode.CBC // Cipher Block Chaining (default)
CryptoJS.mode.CFB // Cipher Feedback
CryptoJS.mode.CTR // Counter mode
CryptoJS.mode.ECB // Electronic Codebook
CryptoJS.mode.OFB // Output Feedback
// Padding Schemes
CryptoJS.pad.Pkcs7 // PKCS#7 padding (default)
CryptoJS.pad.AnsiX923 // ANSI X.923 padding
CryptoJS.pad.Iso10126 // ISO 10126 padding
CryptoJS.pad.NoPadding // No padding
CryptoJS.pad.ZeroPadding // Zero padding
// Formatters
CryptoJS.format.OpenSSL // OpenSSL-compatible format (default)
CryptoJS.format.Hex // Hexadecimal format// Core Types
WordArray {
words: number[]; // Array of 32-bit words
sigBytes: number; // Significant bytes count
toString(encoder?): string; // Convert to string representation
concat(wordArray): WordArray; // Concatenate with another WordArray
}
CipherParams {
ciphertext: WordArray; // Encrypted data
key?: WordArray; // Encryption key (optional)
iv?: WordArray; // Initialization vector (optional)
salt?: WordArray; // Salt value (optional)
algorithm?: Object; // Algorithm reference (optional)
mode?: Object; // Cipher mode (optional)
padding?: Object; // Padding scheme (optional)
blockSize?: number; // Block size (optional)
formatter?: Object; // Output formatter (optional)
toString(formatter?): string; // Serialize to string
}
// Configuration Objects
EncryptionConfig {
iv?: WordArray; // Initialization vector
mode?: Object; // Cipher mode (default: CBC)
padding?: Object; // Padding scheme (default: Pkcs7)
format?: Object; // Output formatter (default: OpenSSL)
}
HasherCfg {
outputLength?: number; // Output length in bits (for SHA-3)
}
KdfCfg {
keySize?: number; // Key size in words (default: 4)
iterations?: number; // Iteration count (default: 1 for EvpKDF, 250000 for PBKDF2)
hasher?: Object; // Hash function (for PBKDF2, default: SHA256)
}