What is the Node.js | crypto.createHash() method?

Node.js

Node.js is an open source runtime environment built on Chrome’s V8 JavaScript engine that allows Developers to build almost anything. It contains several modules.

Let’s have a look at one of the modules, the crypto module.

The crypto module allows for secure credentials to be encapsulated and utilized as part of a secure HTTPS net or http connection.

It also includes wrappers for the hash, hmac, cipher, decipher, sign, and verify methods of OpenSSL.

Let’s have a look at one of the approaches for verification: createHash()

createHash() is a method used to create hashes.

Crypto.createHash() method

The crypto.createHash() method creates and returns a hash object. You can use the supplied algorithm to use this hash object to generate hash digests.

The optional options are used to control the behavior of the stream. The desired output length in bytes is provided for various hash algorithms, such as XOF and ‘shake256’. The createHash() method has a built-in hash class.

The hash class

Hash is the class use to generate hash digests. It’s a stream that can be read and written to. Written data is used to compute the hash.

Use the read() method to receive the computed hash digest after the writable side of the stream is finished. The update and digest techniques from the past are also supported.

Syntax

crypto.createHash(algorithm, [options])

Parameters

The parameters listed above are described below.

Algorithm: This algorithm is used to generate the hash digests. The input type is string.

Options: These are optional parameters used to modify the behavior of the stream.

Use the read() method to receive the computed hash digest after the writable side of the stream is finished.

Example 1

createHash.js – use this command to run the code shown in the example below:

node createHash.js
// crypto.createHash() demo example1
// Importing crypto module
const crypto = require('crypto');
// Deffining the secret key
const secret = 'TutorialsPoint';
// Initializing the createHash method using secret
const hashValue = crypto.createHash('sha256', secret)
// Data to be encoded
.update('Welcome to TutorialsPoint !')
// Defining encoding type
.digest('hex');
// Printing the output
console.log("Hash Obtained is: ", hashValue);

Example 2

Let’s take a look at one more example, createHash2.js:

node createHash2.js
// crypto.createHash() demo example2
// Importing crypto module
const crypto = require('crypto');
const fs = require('fs');
// Getting the current file path
const filename = process.argv[1];
// Creting hash for current path using secret
const hash = crypto.createHash('sha256', "TutorialsPoint");
const input = fs.createReadStream(filename);
input.on('readable', () => {
// Reading single element produced by hash stream.
const val = input.read();
if (val)
hash.update(val);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});

Explanation

We imported the crypto module and the file module in the first line. Then, we created a hashValue variable that takes the result of the crypto.createhash() method. In the fifth line we passed the hashValue through the digest() method that defines the encoding type. Finally, we printed out the hash result.

Applications

Wireless security, processor security, file encryption, and SSL/TLS are just a few of the applications that use the createHash() technique. As well as:

  • WiFi ( as part of WPA2)
  • Mobile apps( such as WhatsApp)
  • VPN implementations
  • Operating system components like file systems

Conclusion

The createHash() technique is a symmetric block cipher algorithm that has its own structure to encrypt and decrypt sensitive data. It is used in hardware and software all around the world.

Free Resources