Open In App

Checksum algorithms in JavaScript

Last Updated : 22 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Example: In this example, we will see the implementation of the checksum algorithm by using a simple checksum.

Javascript




function simpleChecksum(data) {
    let checksum = 0;
 
    // Iterate through each character in the data
    for (let i = 0; i < data.length; i++) {
        // Add the ASCII value of
        //  the character to the checksum
        checksum += data.charCodeAt(i);
    }
 
    // Ensure the checksum is within
    //the range of 0-255 by using modulo
    return checksum % 256;
}
const data = "GeeksForGeeks";
const checksumValue = simpleChecksum(data);
console.log("Simple Checksum: " + checksumValue);


Output

Simple Checksum: 5

CRC (Cyclic Redundancy Check)

  • 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

Example: In this example we will see the implementation of checksum algorithm by CRC method.

Javascript




function calculateCRC(data) {
    const polynomial = 0xEDB88320;
    let crc = 0xFFFFFFFF;
 
    // Iterate through each character in the data
    for (let i = 0; i < data.length; i++) {
        // XOR the current character
        // with the current CRC value
        crc ^= data.charCodeAt(i);
 
        // Perform bitwise operations
        // to calculate the new CRC value
        for (let j = 0; j < 8; j++) {
            crc = (crc >>> 1) ^ (crc & 1 ? polynomial : 0);
        }
    }
 
    // Perform a final XOR operation and return the CRC value
    return crc ^ 0xFFFFFFFF;
}
 
const data = "Geeks For Geeks";
const crcValue = calculateCRC(data);
console.log("CRC-32 Checksum: " +
    crcValue.toString(16).toUpperCase());


Output

CRC-32 Checksum: -2C03EBB6

MD5 (Message Digest Algorithm 5)

  • MD5 is a widely used cryptographic hash function. It generates a fixed-size, 32-character hash value from input data, providing data integrity and digital signature capabilities in various applications.
  • In this approach, we will make a function calculateMD5 that utilizes Node.js’s crypto library to generate an MD5 checksum.
  • It creates an MD5 hash object, updates it with the provided data, and then produces the checksum.
  • Now the resulting checksum, represented in hexadecimal, serves as a unique identifier for the input data.

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

Javascript




const crypto = require("crypto");
 
// Calculate an MD5 checksum for the
// given data using Node.js's crypto library
function calculateMD5(data) {
    // Create an MD5 hash object
    const hash = crypto.createHash("md5");
 
    // Update the hash object with the data
    hash.update(data);
 
    // Generate the MD5 checksum in hexadecimal format
    return hash.digest("hex");
}
 
const data = "Geeks For Geeks";
const md5Value = calculateMD5(data);
console.log("MD5 Checksum: " + md5Value);


Output

MD5 Checksum: c5ea9d3ffad49b5874ca5a4da1d3c86e

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads