SHA-256 (Secure Hash Algorithm 256-bit)

  • SHA-256 is a robust cryptographic hash function. It computes a fixed-size, 64-character hash value from data, offering strong data integrity and security.
  • Make a calculateSHA256 function that employs Node.js’s crypto library to compute a SHA-256 checksum.
  • It initializes a SHA-256 hash object, incorporates the provided data, and then produces the checksum.
  • SHA-256 is a robust cryptographic hash function widely used for data integrity verification.
  • Now the resulting checksum, presented in hexadecimal form, acts as a secure representation of the input data.

Example: In this example we will see the implementation of checksum algorithm by using SHA-256.

Javascript




const crypto = require("crypto");
 
// Calculate a SHA-256 checksum for
// the given data using Node.js's crypto library
function calculateSHA256(data) {
    // Create a SHA-256 hash object
    const hash = crypto.createHash("sha256");
 
    // Update the hash object with the data
    hash.update(data);
 
    // Generate the SHA-256 checksum
    // in hexadecimal format
    return hash.digest("hex");
}
 
const data = "HGeeks For Geeks";
const sha256Value = calculateSHA256(data);
console.log("SHA-256 Checksum: " + sha256Value);


Output

SHA-256 Checksum: c41f05cb05c78ab992132d4c4335774303e7a8e85600362bd6b5cdadf14229a5


Checksum algorithms in JavaScript

Checksum algorithms are fundamental techniques used in data communication and storage to ensure the integrity of data. The primary purpose of a checksum algorithm is to generate a fixed-size value (the checksum) based on the content of a dataset. They are essential for detecting errors, unauthorized changes, or corruption in data during transmission, saving files, or other data-handling processes.

These are the following techniques for this:

Table of Content

  • Simple Checksum
  • CRC (Cyclic Redundancy Check)
  • MD5 (Message Digest Algorithm 5)
  • SHA-256 (Secure Hash Algorithm 256-bit)

Similar Reads

Simple Checksum

It is a basic data integrity validation method. It generates a compact code to confirm if data is accurate, but it’s simple and limited in detecting errors. In this approach, we will make a function simpleChecksum that calculates a checksum by summing the ASCII values of characters in the input data. Now iterates through each character and accumulates their ASCII values into the checksum variable. Now to ensure the checksum stays within the 0-255 range, it uses modulo arithmetic....

CRC (Cyclic Redundancy Check)

...

MD5 (Message Digest Algorithm 5)

CRC (Cyclic Redundancy Check) is a more robust error-checking method. It computes a short, fixed-size value to confirm data accuracy. CRC is widely used in data communication and storage for error detection. The calculateCRC function computes a CRC-32 checksum using the polynomial 0xEDB88320. Now iterates through each character, XORing it with the current CRC value, and performs bitwise operations. The algorithm ensures data integrity by incorporating bitwise XOR and a specific polynomial. Now final result, represented as a hexadecimal string, serves as a CRC-32 checksum for the given data...

SHA-256 (Secure Hash Algorithm 256-bit)

...