Skip to main content

Comparison of SHA Hash Algorithms

· 4 min read

The Secure Hash Algorithm (SHA) family is a set of cryptographic hash functions published by the National Institute of Standards and Technology (NIST). These algorithms take input data and produce a fixed-size hash value (digest) that represents the original data. SHA algorithms are widely used for data integrity verification, digital signatures, and security applications.

JavaScript Web Crypto API

JavaScript Web Crypto API supports SHA-1 SHA-256 SHA-384 SHA-512

// convert to hex text
const bufferToHex = (buffer) => {
const byteArray = new Uint8Array(buffer);
return Array.from(byteArray)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('');
};

const textInput = "hello";
const encoder = new TextEncoder();
const data = encoder.encode(textInput);

// Web Crypto API supports SHA-1 SHA-256 SHA-384 SHA-512
const hashAlgorithm = "SHA-256";
const hashBuffer = crypto.subtle.digest(hashAlgorithm, data);
const hashHex = bufferToHex(hashBuffer);