Node.js crypto.createHmac() Method Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The crypto.createHmac() method is used to create an Hmac object that uses the stated 'algorithm' and 'key'.Syntax:crypto.createHmac( algorithm, key, options )Parameters: This method accepts three parameters as mentioned above and described below:algorithm: It is dependent on the accessible algorithms which are favored by the version of OpenSSL on the platform. It returns string. The examples are sha256, sha512, etc.key: It is the HMAC key which is used to create the cryptographic HMAC hash. It returns string, Buffer, TypedArray, DataView, or KeyObject. And if it is a KeyObject, then its type must be secret.options: It is optional parameter and used to control stream behavior. It returns an object.Return Type: It returns Hmac object.FeaturesGenerates a unique hash based on the content and a secret key.Supports various cryptographic algorithms such as 'sha256', 'sha1', 'md5', etc.Can be used with streams for processing large amounts of data.Below examples illustrate the use of crypto.createHmac() method in Node.js: Example 1: This example demonstrates using the crypto.createHmac() method to generate an HMAC hash of the string "GeeksforGeeks" using the SHA-256 algorithm and a secret key, then outputting the hash in hexadecimal format. JavaScript // Node.js program to demonstrate the // crypto.createHmac() method // Includes crypto module const crypto = require('crypto'); // Defining key const secret = 'GfG'; // Calling createHmac method const hash = crypto.createHmac('sha256', secret) .update('GeeksforGeeks') // updating data .digest('hex'); // Encoding to be used // Displays output console.log(hash); Output:a08116905e92633e4f30eefd1276206b259305c8783642fc5b7f51c089187939Example 2: This example demonstrates using the crypto.createHmac() method to generate an HMAC hash of the contents of the current file, read via a stream, and then outputs the hash along with the filename. JavaScript // Node.js program to demonstrate the // crypto.createHmac() method // Defining myfile const myfile = process.argv[1]; // Includes crypto and fs module const crypto = require('crypto'); const fs = require('fs'); // Creating Hmac const creathmac = crypto.createHmac('sha1', 'CS-Portal!'); // Creating read stream const readfile = fs.createReadStream(myfile); readfile.on('readable', () => { // Calling read method to read data const data = readfile.read(); if (data) { // Updating creathmac.update(data); } else { // Encoding and displaying filename console.log("The hmac object returns:", `${creathmac.digest('hex')} ${myfile}`); } }); console.log("Program done!"); console.log(); Output:Program done!The hmac object returns: 4605d44703c2620fc2574c9a9216bd3267457324 /run_dir/interp.jsSummaryThe crypto.createHmac() method in Node.js is a powerful tool for generating HMACs, ensuring the integrity and authenticity of data. It supports various algorithms and integrates seamlessly with other Node.js modules, making it a versatile choice for secure data handling in your applications.Reference: https://nodejs.org/api/crypto.html#crypto_crypto_createhmac_algorithm_key_options Comment More infoAdvertise with us nidhi1352singh Follow Improve Article Tags : Web Technologies Node.js Node.js-crypto-module Similar Reads Node.js cipher.final() Method The cipher.final() method in Node.js is used to signal to the cipher object that the encryption or decryption process is complete. This method must be called after all data has been passed to the cipher object using the cipher.update() method. The cipher.final() method returns the remaining encrypte 2 min read Node.js cipher.update() Method The cipher.update() method is an inbuilt application programming interface of class Cipher within crypto module which is used to update the cipher with data according to the given encoding format. Syntax: const cipher.update(data[, inputEncoding][, outputEncoding]) Parameters: This method takes the 2 min read Node.js crypto.getCiphers() Method The crypto.getCiphers() method returns an array the names of all the supported cipher algorithms. Syntax: crypto.getCiphers() Parameters: This method doesn't accepts any parameters. Return Value: It returns the names of all the supported cipher algorithms. Below example illustrate the use of crypto. 2 min read Node.js crypto.createECDH() Method The crypto.createECDH() method is an inbuilt application programming interface of crypto module which is used to create an Elliptic Curve Diffie-Hellman i.e, (ECDH) key exchange object with the help of a predefined curve which is defined by the curveName string. Moreover you can use crypto.getCurves 2 min read Node.js crypto.createDecipheriv() Method The crypto.createDecipheriv() method is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector i.e, (iv). Syntax: crypto.createDecipheriv( algorithm, key, iv, options ) Parameters: This method 3 min read Node crypto.createCipheriv() Method The crypto.createCipheriv() method is an inbuilt application programming interface of the crypto module which is used to create a Cipher object, with the stated algorithm, key, and initialization vector (iv).Syntax: crypto.createCipheriv( algorithm, key, iv, options )Parameters: This method accepts 2 min read Node.js crypto.getDiffieHellman() Method The crypto.getDiffieHellman() method is used to create a predefined DiffieHellmanGroup key exchange object. Here, the favored groups are 'modp1', 'modp2', 'modp5', which are defined in RFC 2412 and 'modp14', 'modp15', 'modp16', 'modp17', 'modp18', defined in RFC 3526. Syntax: crypto.getDiffieHellman 2 min read Node.js crypto.pbkdf2() Method The crypto.pbkdf2() method gives an asynchronous Password-Based Key Derivation Function 2 i.e. (PBKDF2) implementation. Moreover, a particular HMAC digest algorithm which is defined by digest is implemented to derive a key of the required byte length (keylen) from the stated password, salt, and iter 2 min read Node crypto.createHash() Method The crypto.createHash() method is used to create a Hash object that can be used to create hash digests by using the stated algorithm. Syntax:crypto.createHash( algorithm, options )Parameters: This method accepts two parameters as mentioned above and described below:algorithm: It is dependent on the 2 min read Node.js crypto.createHmac() Method The crypto.createHmac() method is used to create an Hmac object that uses the stated 'algorithm' and 'key'.Syntax:crypto.createHmac( algorithm, key, options )Parameters: This method accepts three parameters as mentioned above and described below:algorithm: It is dependent on the accessible algorithm 2 min read Like