Cryptographic hash functions for data integrity, digital signatures, and secure data verification. All hash functions in CryptoJS follow a consistent pattern: they accept string or WordArray input and return a WordArray that can be converted to various string formats.
MD5 hash algorithm (not recommended for security-critical applications due to vulnerabilities).
/**
* Computes MD5 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.MD5(message)Usage Example:
var CryptoJS = require("crypto-js");
var hash = CryptoJS.MD5("Hello World");
console.log(hash.toString()); // "b10a8db164e0754105b7a99be72e3fe5"
console.log(hash.toString(CryptoJS.enc.Base64)); // "sQqNsWTgdUEFt6mb5y4/5Q=="SHA-1 hash algorithm (deprecated for security-critical applications).
/**
* Computes SHA-1 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.SHA1(message)SHA-224 hash algorithm, producing 224-bit hash values.
/**
* Computes SHA-224 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.SHA224(message)SHA-256 hash algorithm, producing 256-bit hash values. Recommended for most applications.
/**
* Computes SHA-256 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.SHA256(message)Usage Example:
var CryptoJS = require("crypto-js");
var hash = CryptoJS.SHA256("Hello World");
console.log(hash.toString()); // "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"
// Hash WordArray data
var data = CryptoJS.enc.Utf8.parse("Hello World");
var hash2 = CryptoJS.SHA256(data);
console.log(hash2.toString() === hash.toString()); // trueSHA-384 hash algorithm, producing 384-bit hash values.
/**
* Computes SHA-384 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.SHA384(message)SHA-512 hash algorithm, producing 512-bit hash values.
/**
* Computes SHA-512 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.SHA512(message)SHA-3 (Keccak) hash algorithm with configurable output length.
/**
* Computes SHA-3 hash of the input message
* @param message - String or WordArray to hash
* @param cfg - Configuration object with optional outputLength
* @returns WordArray containing the hash
*/
CryptoJS.SHA3(message, cfg)Usage Example:
var CryptoJS = require("crypto-js");
// Default SHA-3 (512-bit output)
var hash = CryptoJS.SHA3("Hello World");
console.log(hash.toString());
// SHA-3 with custom output length
var hash224 = CryptoJS.SHA3("Hello World", { outputLength: 224 });
var hash256 = CryptoJS.SHA3("Hello World", { outputLength: 256 });
var hash384 = CryptoJS.SHA3("Hello World", { outputLength: 384 });
var hash512 = CryptoJS.SHA3("Hello World", { outputLength: 512 });RIPEMD-160 hash algorithm, producing 160-bit hash values.
/**
* Computes RIPEMD-160 hash of the input message
* @param message - String or WordArray to hash
* @returns WordArray containing the hash
*/
CryptoJS.RIPEMD160(message)var CryptoJS = require("crypto-js");
var message = "Hello World";
var hash = CryptoJS.SHA256(message);
// Different output formats
console.log("Hex:", hash.toString()); // Default hex
console.log("Base64:", hash.toString(CryptoJS.enc.Base64)); // Base64
console.log("Words:", hash.words); // Raw word array
console.log("Bytes:", hash.sigBytes); // Significant bytes countvar CryptoJS = require("crypto-js");
// Create hasher instance for progressive hashing
var sha256 = CryptoJS.algo.SHA256.create();
sha256.update("Hello");
sha256.update(" ");
sha256.update("World");
var hash = sha256.finalize();
console.log(hash.toString()); // Same result as CryptoJS.SHA256("Hello World")var CryptoJS = require("crypto-js");
var fs = require("fs");
function hashFile(filePath) {
var data = fs.readFileSync(filePath, 'utf8');
return CryptoJS.SHA256(data).toString();
}
// Usage
var fileHash = hashFile("./document.txt");
console.log("File SHA-256:", fileHash);// Hash Configuration (for SHA-3)
HasherCfg {
outputLength?: number; // Output length in bits (224, 256, 384, 512)
}
// Hash Algorithm Interface
Hasher {
update(messageUpdate): Hasher; // Add data to hash
finalize(messageUpdate?): WordArray; // Compute final hash
reset(): void; // Reset hasher state
clone(): Hasher; // Clone hasher instance
}