Crypto in Node.js is a module that implements an algorithm for data encryption and decryption. It is used for security purposes such as user authentication, where the password is encrypted and stored in the database.
A hash is a fixed-length string of bits created with algorithms and procedures from any arbitrary source data block. The properties of
Fixed length: This means that the length of the hash remains constant regardless of the input. SHA-256 hashes, for example, are always 256 bits long, regardless of whether the input data is a few bits or a few terabytes.
Deterministic: We should expect to be able to calculate the same hash for the same input. As a result, hashes are valuable for checksums.
Collision-resistant: When an identical hash is created for two different input blocks of data, it is called a collision. Hash algorithms are made to be highly rare to cause collisions. The importance of this feature is dependent on the use case.
Unidirectional: Good hash algorithms are easy to apply, but hard to undo. In other words, given a hash, we cannot reasonably find out what the original piece of data was.
crypto.createHash()
?The crypto.createHash()
method is part of Node’s crypto module. It is a method that allows you to calculate a hash. It returns a Hash object that is used to generate hash digests with the algorithm sent in as an argument.
crypto.createHash( algorithm, [options] )
As shown above, this method accepts two parameters.
The algorithm is dependent on preferred accessible algorithms by the platform’s version of OpenSSL. It returns a string. Examples include sha256, sha512, and so on.
The options parameter is an optional parameter that is used to control the behaviour of the stream. It returns an object. Furthermore, for XOF hash functions such as sha256
, the output length option can be used to determine the required output length in bytes.
const crypto = require('crypto');const words = 'What is Node Crypto.createHash()';let hashObj = [{algorithm: 'sha256', digestFormat: 'base64'},{algorithm: 'sha256', digestFormat: 'base64'},{algorithm: 'sha256', digestFormat: 'hex'},{algorithm: 'sha256', digestFormat: 'hex'},{algorithm: 'sha256', digestFormat: 'hex'},{algorithm: 'sha256', digestFormat: 'binary'}]for (const {algorithm, digestFormat} of hashObj){// Calling createHash methodconst hash = crypto.createHash(algorithm)// updating data.update(words)// Encoding to be used.digest(digestFormat);console.log(`${algorithm}-${digestFormat} gives`, hash)}
.update()
The update method is used to push data that will later be converted to a hash. The update can be called multiple times to ingest streaming data, such as buffers from a file read stream.
.digest()
The digest method accepts an argument that represents the output format, which can be binary
, hex
, or base64
. If nothing is passed, or anything aside from the three mentioned above, it will return a buffer.
const crypto = require('crypto');const words = __filename;let hashObj = [{algorithm: 'sha256', digestFormat: 'base64'},{algorithm: 'sha512', digestFormat: 'base64'},{algorithm: 'sha256', digestFormat: 'hex'},{algorithm: 'sha512', digestFormat: 'hex'},{algorithm: 'sha256', digestFormat: 'binary'},{algorithm: 'sha512', digestFormat: 'binary'},]for (const {algorithm, digestFormat} of hashObj){const hash = crypto.createHash(algorithm).update(words).digest(digestFormat);console.log(`${algorithm}-${digestFormat} gives`, hash.length)}
Although the length is partly dependent on the output format specified in the digest, the 'sha256'
results in a shorter hash when compared to what the 'sha512'
algorithm provides. Other hashing algorithms are 'md4'
, 'md5'
, 'sha1'
, 'sha384'
, 'sha512_224'
, 'sha512_256'
and 'ripemd160'
.
Confidentiality: Confidentiality is the most frequently mentioned purpose. The meaning of a message is encoded when the sender encrypts the communication with a cryptographic key.
Data authenticity: Authenticity ensures the sender and recipient can verify each other’s identities and the destination of the message.
Data integrity: Hashing is used to create a unique message digest that is sent with the message.