The Wayback Machine - https://web.archive.org/web/20240830143459/https://www.geeksforgeeks.org/node-js-crypto-createhash-method/
Open In App

Node crypto.createHash() Method

Last Updated : 15 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 accessible algorithms that are favored by the version of OpenSSL on the platform. It returns a string. The examples are sha256, sha512, etc.
  • options: It is an optional parameter and is used to control stream behavior. It returns an object. Moreover, For XOF hash functions like ‘shake256’, the option outputLength can be used to determine the required output length in bytes.

Return Type: It returns Hash object. 

Example 1: Below examples illustrate the use of crypto.createHash() method in Node.js:

Javascript




// Node.js program to demonstrate the    
// crypto.createHash() method
 
// Includes crypto module
const crypto = require('crypto');
 
// Defining key
const secret = 'Hi';
 
// Calling createHash method
const hash = crypto.createHash('sha256', secret)
 
    // updating data
    .update('How are you?')
 
    // Encoding to be used
    .digest('hex');
 
// Displays output
console.log(hash);


Output:

df287dfc1406ed2b692e1c2c783bb5cec97eac53151ee1d9810397aa0afa0d89

Example 2: Below examples illustrate the use of crypto.createHash() method in Node.js:

Javascript




// Node.js program to demonstrate the    
// crypto.createHash() method
 
// Defining filename
const filename = process.argv[1];
 
// Includes crypto and fs module
const crypto = require('crypto');
const fs = require('fs');
 
// Creating Hash
const hash =
    crypto.createHash('sha256', 'Geeksforgeeks');
 
// Creating read stream
const input = fs.createReadStream(filename);
 
input.on('readable', () => {
 
    // Calling read method to read data
    const data = input.read();
    if (data) {
        // Updating
        hash.update(data);
    }
    else {
        // Encoding and displaying filename
        console.log(`
        ${hash.digest('base64')} ${filename}
        `);
    }
});
console.log("Program done!");


Output:

Program done!
n95mt3468ZzAIwu/bbNU7dej6CoFkDRcNaJo7rGpLF4= index.js

We have a Cheat Sheet on Node crypto methods where we covered all the crypto methods to check those please go through Node Crypto Complete Reference article.



Previous Article
Next Article

Similar Reads

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( groupName ) Parameters: This method accept singl
2 min read
Node.js crypto.createVerify() Method
The crypto.createVerify() method is used to create a Verify object that uses the stated algorithm. Moreover, you can use crypto.getHashes() to access the names of all the available signing algorithms. Syntax: crypto.createVerify( algorithm, options ) Parameters: This method accept two parameters as mentioned above and described below: algorithm: It
2 min read
Node.js crypto.createSign() Method
The crypto.createSign() method is used to create a Sign object that uses the stated algorithm. Moreover, you can use crypto.getHashes() method to access the names of all the available digest algorithms. Syntax: crypto.createSign( algorithm, options ) Parameters: This method accept two parameters as mentioned above and described below: algorithm: It
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.getCiphers() method in Node.js: Example: // Node.j
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 iterations. Syntax: crypto.pbkdf2( password, salt, ite
2 min read
Node.js crypto.pbkdf2Sync() Method
The crypto.pbkdf2Sync() method gives a synchronous 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 iterations. Syntax: crypto.pbkdf2Sync( password, sal
2 min read
Node.js crypto.getCurves() Method
The crypto.getCurves() method is an inbuilt application programming interface of the crypto module which is used to display an array with the names of all the supported elliptic curves. Syntax: crypto.getCurves() Parameters: This method doesn't accept any parameters. Return Value: This method returns the name of all the supported elliptic curves. B
2 min read
Node.js crypto.getHashes() Method
The crypto.getHashes() method is an inbuilt application programming interface of crypto module which is used to display the names of all the supported hash algorithms in an array. Syntax: crypto.getHashes() Parameters: This method doesn't accept any parameters. Return Value: It returns the name of all the supported hash algorithms. Below example il
1 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 accept four parameters as mentioned above and desc
3 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() method in order to return the list of available
2 min read
Node.js crypto.createDiffieHellmanGroup() Method
The crypto.createDiffieHellmanGroup() method is an inbuilt application programming interface of the crypto module which is used to create a DiffieHellmanGroup. Syntax: crypto.createDiffieHellmanGroup( name ) Parameters: This method accepts single parameters name which is of type string. Return Value: It returns the DiffieHellmanGroup. The below exa
1 min read
Node.js crypto.privateDecrypt() Method
The crypto.privateDecrypt() method is used to decrypt the content of the buffer with privateKey.buffer which was previously encrypted using the corresponding public key, i.e. crypto.publicEncrypt(). Syntax: crypto.privateDecrypt( privateKey, buffer ) Parameters: This method accepts two parameters as mentioned above and described below: privateKey:
3 min read
Node.js crypto.privateEncrypt() Method
The crypto.privateEncrypt() method is used to encrypt the stated content of the buffer with the parameter 'privateKey'. Syntax: crypto.privateEncrypt( privateKey, buffer ) Parameters: This method accept two parameters as mentioned above and described below: privateKey: It can hold Object, string, Buffer, or KeyObject type of data. key: It is a 'PEM
3 min read
Node.js crypto.publicEncrypt() Method
The crypto.publicEncrypt() method is an inbuilt application programming interface of the crypto module which is used to encrypt the stated content of the buffer with the parameter 'key'. Syntax: crypto.publicEncrypt( key, buffer ) Parameters: This method accept two parameters as mentioned above and described below: key: This parameter holds Object,
3 min read
Node.js crypto.createDiffieHellman(primeLength, generator) Method
The crypto.createDiffieHellman() method is used to create a Diffie-Hellman key exchange object. Also, creates prime of primeLength bits with the help of an optional specific numeric generator. Moreover, if the generator is not defined, then the value 2 is used. Syntax: crypto.createDiffieHellman( primeLength, generator ) Parameters: This method acc
2 min read
Node.js crypto.createDiffieHellman(prime, primeEncoding, generator, generatorEncoding) Method
The crypto.createDiffieHellman() method is used to create a DiffieHellman key exchange object by using the stated prime and an optional specified generator.Syntax: crypto.createDiffieHellman( prime, primeEncoding, generator, generatorEncoding ) Parameters: This method accept four parameters as mentioned above and described below: prime: It can hold
2 min read
Node.js crypto.publicDecrypt() Method
The crypto.publicDecrypt() method is used to decrypt the content of the buffer with a key which was previously encrypted using the corresponding private key, i.e. crypto.privateEncrypt() method. Syntax: crypto.publicDecrypt( key, buffer ) Parameters: This method accepts two parameters as mentioned above and described below: key: It is of type Objec
3 min read
Node.js crypto.generateKeyPairSync() Method
The crypto.generateKeyPairSync() method is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type. For example, the currently supported key types are RSA, DSA, EC, Ed25519, Ed448, X25519, X448, and DH. Moreover, if option's publicKeyEncoding or privateKeyEncoding is st
4 min read
Node.js crypto.generateKeyPair() Method
The crypto.generateKeyPair() method is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type. For example, the currently supported key types are RSA, DSA, EC, Ed25519, Ed448, X25519, X448, and DH. Moreover, if option's publicKeyEncoding or privateKeyEncoding is stated
4 min read
Node.js crypto.scrypt() Method
The crypto.scrypt() method is an inbuilt application programming interface of the crypto module which is used to enable an implementation of an asynchronous script. Where scrypt is a password-based key derivation function. It is intended to be costly computationally plus memory-wise. So, the brute-force attacks are made unsuccessful. Syntax: crypto
4 min read
Node.js crypto.randomFill() Method
The crypto.randomFill() method is same as crypto.randomBytes() method but the only difference is that here the first argument is buffer that will be filled and it also has a callback function passed as an argument. However, if a callback function is not available then the error is thrown. Syntax: crypto.randomFill( buffer, offset, size, callback )
2 min read
Node.js crypto.randomFillSync() Method
The crypto.randomFillSync() method is an inbuilt application programming interface of crypto module which is used to return the object passed as buffer argument.Syntax: crypto.randomFillSync( buffer, offset, size ) Parameters: This method accept three parameters as mentioned above and described below: buffer This parameter holds Buffer, TypedArray,
2 min read
Node.js crypto.randomInt() Method
The Crypto.randomInt method in Node.js is an inbuilt application programming interface of the crypto module which is used to create a random integer synchronously or asynchronously based on our usage. Syntax: crypto.randomInt([min, ] max [, callback]) Parameters: This method accepts three parameters as mentioned above and described below. min: An o
2 min read
Node.js crypto.hkdfSync( ) Method
This method provides a synchronous HMAC-based Extract-and-Expand Key Derivation Function key derivation. Key of keylen bytes is derived using digest, given key, salt and info. Syntax: crypto.hkdfSync(digest, key, salt, info, keylen) Parameters: This method has five parameters. digest: It must be string. key: Minimum lengths — 1 byte, It is of type
2 min read
Node.js crypto.getCipherInfo() Method
Node.js crypto.getCipherInfo() method in node.js provides detailed information about the cipher. This method is very useful in case you want to get information about the different parameters like mode, key length, block size, initialization vector length, etc. Syntax: The following is the syntax for getting information about ciphers: // Import cryp
3 min read
Node.js crypto.secureHeapUsed() Method
The crypto.secureHeapUsed() method in Node.js is used to get the amount of memory used by the secure heap of the running Node.js process. The secure heap is a section of memory that is reserved for cryptographic operations in the Node.js process. This memory is separate from the main heap, which is used for general-purpose memory allocation in the
2 min read
Node crypto.randomBytes() Method
The crypto.randomBytes() method is used to generate cryptographically well-built artificial random data and the number of bytes to be generated in the written code. Syntax: crypto.randomBytes( size, callback )Parameters: This method accepts two parameters as mentioned above and described below: size: It is of type number which indicates the number
2 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 four parameters as mentioned above and described b
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 algorithms which are favored by the version of OpenSSL on
3 min read
Node JS | Password Hashing with Crypto module
In real-life applications with User authentication functionality, storing the user passwords as the original string in the database is not practical. Still, it is good practice to hash the password and then store them in the database. Crypto module for Node JS helps developers to hash user passwords. Examples: Original Password : portalforgeeks Has
5 min read