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.
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.
crypto.createHash(algorithm, [options])
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.
createHash.js
–
use this command to run the code shown in the example below:
node createHash.js
// crypto.createHash() demo example1// Importing crypto moduleconst crypto = require('crypto');// Deffining the secret keyconst secret = 'TutorialsPoint';// Initializing the createHash method using secretconst hashValue = crypto.createHash('sha256', secret)// Data to be encoded.update('Welcome to TutorialsPoint !')// Defining encoding type.digest('hex');// Printing the outputconsole.log("Hash Obtained is: ", hashValue);
Let’s take a look at one more example, createHash2.js
:
node createHash2.js
// crypto.createHash() demo example2// Importing crypto moduleconst crypto = require('crypto');const fs = require('fs');// Getting the current file pathconst filename = process.argv[1];// Creting hash for current path using secretconst 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}`);}});
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.
Wireless security, processor security, file encryption, and SSL/TLS are just a few of the applications that use the createHash()
technique. As well as:
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.