Through the use of an algorithm, the crypto.createHash()
method creates a Hash object that is then used to create hash digests. Examples of the algorithm it takes as input are 'sha256'
, 'sha512'
.
const crypto = require('crypto')const code = 'Hello World'const Hash = crypto.createHash('sha256', code).update('How are you?').digest('hex')console.log(Hash)
The crypto module is imported from NodeJS. crypto.createHash()
takes in two arguments: 1) the algorithm, and 2) the data that needs to be hashed.
The update
method works with the digest
method to push data to later be turned into a hash.
The update
method can be called multiple times to ingest streaming data, such as buffers from a file read stream. The argument for the digest
method represents the output format, and may either be “hex”,“binary” or “base64”. It defaults to binary.
This is used for security purposes like user authentication, in which we are storing the password in a database in the encrypted form by encryption and decryption.