const crypto = require('crypto');
// Data to authenticate
const data = 'Node.js Crypto HMAC Example';
// Secret key
const secretKey = 'my-secret-key';
// Function to create HMAC with different algorithms
function createHmacWithAlgorithm(algorithm, data, key) {
const hmac = crypto.createHmac(algorithm, key);
hmac.update(data);
return hmac.digest('hex');
}
// Test various HMAC algorithms
const algorithms = ['md5', 'sha1', 'sha256', 'sha512', 'sha3-256', 'sha3-512'];
console.log(`Data: "${data}"`);
console.log(`Secret Key: "${secretKey}"`);
console.log('------------------------------------');
algorithms.forEach(algorithm => {
try {
const digest = createHmacWithAlgorithm(algorithm, data, secretKey);
console.log(`HMAC-${algorithm}: ${digest}`);
console.log(`Length: ${digest.length / 2} bytes (${digest.length * 4} bits)`);
console.log('------------------------------------');
} catch (error) {
console.log(`HMAC-${algorithm}: Not supported - ${error.message}`);
console.log('------------------------------------');
}
});
Data: "Node.js Crypto HMAC Example" Secret Key: "my-secret-key" ------------------------------------ HMAC-md5: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7 Length: 16 bytes (128 bits) ------------------------------------ HMAC-sha1: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1 Length: 20 bytes (160 bits) ------------------------------------ HMAC-sha256: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3 Length: 32 bytes (256 bits) ------------------------------------ HMAC-sha512: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b31a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3 Length: 64 bytes (512 bits) ------------------------------------ HMAC-sha3-256: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3 Length: 32 bytes (256 bits) ------------------------------------ HMAC-sha3-512: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b31a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3 Length: 64 bytes (512 bits) ------------------------------------