How to useCrypto library’s createHash() method with SHA-256 algorithm in Javascript

The Crypto library in Node.js provides a createHash() method that allows generating hash digests using various algorithms, including SHA-256. This method takes the algorithm name as an argument and returns a Hash object, which can then be used to update the hash with data and obtain the hash digest.

Example:

JavaScript
const crypto = require('crypto');

function createSHA256Hash(inputString) {
    const hash = crypto.createHash('sha256');
    hash.update(inputString);
    return hash.digest('hex');
}

// Example usage
const inputString = "Hello, World!";
const hashValue = createSHA256Hash(inputString);
console.log(hashValue); // Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Output:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824




How to create hash from string in JavaScript ?

To create a unique hash from a specific string, it can be implemented using its own string-to-hash converting function. It will return the hash equivalent of a string. Also, a library named Crypto can be used to generate various types of hashes like SHA1, MD5, SHA256, and many more. 

These are the following methods to Create Hash from String:

Table of Content

  • Using JavaScript charCodeAt() method
  • Using crypto.createHash() method
  • Using JavaScript String’s reduce() Method
  • Using bitwise XOR operation
  • Using Crypto library’s createHash() method with SHA-256 algorithm

Note: The hash value of an empty string is always zero. 

Similar Reads

Using JavaScript charCodeAt() method

The JavaScript str.charCodeAt() method returns a Unicode character set code unit of the character present at the index in the string specified as the argument. The index number ranges from 0 to n-1, where n is the string’s length....

Using crypto.createHash() method

The crypto.createHash() method is used to create a Hash object that can be used to create hash digests by using the stated algorithm....

Using JavaScript String’s reduce() Method

The reduce() method in JavaScript applies a function to each element of the array (or in this case, each character of the string) to reduce the array to a single value. In this method, we can accumulate a hash value by iteratively processing each character’s Unicode code point and incorporating it into the hash....

Using bitwise XOR operation

Using a bitwise XOR operation to generate a hash from a string. It iterates over each character, XORs its Unicode value with the current hash, updating it. This approach offers simplicity and efficiency in creating a unique hash....

Approach: Using Crypto library’s createHash() method with SHA-256 algorithm

The Crypto library in Node.js provides a createHash() method that allows generating hash digests using various algorithms, including SHA-256. This method takes the algorithm name as an argument and returns a Hash object, which can then be used to update the hash with data and obtain the hash digest....