Checksum algorithms in JavaScript
Last Updated :
22 Nov, 2023
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;
for (let i = 0; i < data.length; i++) {
checksum += data.charCodeAt(i);
}
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;
for (let i = 0; i < data.length; i++) {
crc ^= data.charCodeAt(i);
for (let j = 0; j < 8; j++) {
crc = (crc >>> 1) ^ (crc & 1 ? polynomial : 0);
}
}
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" );
function calculateMD5(data) {
const hash = crypto.createHash( "md5" );
hash.update(data);
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" );
function calculateSHA256(data) {
const hash = crypto.createHash( "sha256" );
hash.update(data);
return hash.digest( "hex" );
}
const data = "HGeeks For Geeks" ;
const sha256Value = calculateSHA256(data);
console.log( "SHA-256 Checksum: " + sha256Value);
|
Output
SHA-256 Checksum: c41f05cb05c78ab992132d4c4335774303e7a8e85600362bd6b5cdadf14229a5
Share your thoughts in the comments
Please Login to comment...