Audited & minimal 0-dependency JS implementation of SHA, RIPEMD, BLAKE, HMAC, HKDF, PBKDF & Scrypt
SHA-2 (Secure Hash Algorithm 2) is a family of cryptographic hash functions standardized by NIST. These are the most widely used hash functions in modern cryptography, appearing in TLS/SSL, cryptocurrencies, digital signatures, and data integrity verification.
import { sha256, sha384, sha512, sha224, sha512_224, sha512_256 } from '@noble/hashes/sha2.js';32-byte (256-bit) hash function. The most commonly used variant, balancing security and performance.
/**
* Computes SHA-256 hash of input data
* @param msg - Input data as Uint8Array
* @returns 32-byte hash digest
*/
function sha256(msg: Uint8Array): Uint8Array;
// Properties
sha256.outputLen: 32; // Output length in bytes
sha256.blockLen: 64; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA256>;Usage:
import { sha256 } from '@noble/hashes/sha2.js';
import { bytesToHex } from '@noble/hashes/utils.js';
// Simple hash
const hash = sha256(Uint8Array.from([0xca, 0xfe]));
console.log(bytesToHex(hash)); // Output: 32-byte hex string
// Incremental hashing
const hasher = sha256.create();
hasher.update(Uint8Array.from([0x10, 0x20]));
hasher.update(Uint8Array.from([0x30, 0x40]));
const result = hasher.digest();48-byte (384-bit) hash function. A truncated variant of SHA-512.
/**
* Computes SHA-384 hash of input data
* @param msg - Input data as Uint8Array
* @returns 48-byte hash digest
*/
function sha384(msg: Uint8Array): Uint8Array;
// Properties
sha384.outputLen: 48; // Output length in bytes
sha384.blockLen: 128; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA384>;64-byte (512-bit) hash function. Provides higher security margin and quantum resistance.
/**
* Computes SHA-512 hash of input data
* @param msg - Input data as Uint8Array
* @returns 64-byte hash digest
*/
function sha512(msg: Uint8Array): Uint8Array;
// Properties
sha512.outputLen: 64; // Output length in bytes
sha512.blockLen: 128; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA512>;28-byte (224-bit) hash function. A truncated variant of SHA-256.
/**
* Computes SHA-224 hash of input data
* @param msg - Input data as Uint8Array
* @returns 28-byte hash digest
*/
function sha224(msg: Uint8Array): Uint8Array;
// Properties
sha224.outputLen: 28; // Output length in bytes
sha224.blockLen: 64; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA224>;32-byte (256-bit) hash function. A truncated and specially initialized variant of SHA-512, offering better performance on 64-bit platforms than SHA-256 while maintaining 256-bit output.
/**
* Computes SHA-512/256 hash of input data
* @param msg - Input data as Uint8Array
* @returns 32-byte hash digest
*/
function sha512_256(msg: Uint8Array): Uint8Array;
// Properties
sha512_256.outputLen: 32; // Output length in bytes
sha512_256.blockLen: 128; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA512_256>;28-byte (224-bit) hash function. A truncated and specially initialized variant of SHA-512.
/**
* Computes SHA-512/224 hash of input data
* @param msg - Input data as Uint8Array
* @returns 28-byte hash digest
*/
function sha512_224(msg: Uint8Array): Uint8Array;
// Properties
sha512_224.outputLen: 28; // Output length in bytes
sha512_224.blockLen: 128; // Block length in bytes
// Create incremental hasher
function create(): Hash<SHA512_224>;All SHA-2 functions return instances implementing the Hash interface when using .create():
interface Hash<T> {
blockLen: number;
outputLen: number;
/**
* Updates hash with new data
* @param buf - Data to hash
* @returns this for chaining
*/
update(buf: Uint8Array): this;
/**
* Finalizes hash and returns digest
* @returns Hash digest as Uint8Array
*/
digest(): Uint8Array;
/**
* Finalizes hash and writes digest to provided buffer
* @param buf - Output buffer (must be >= outputLen)
*/
digestInto(buf: Uint8Array): void;
/**
* Destroys internal state (zeros memory)
*/
destroy(): void;
/**
* Creates independent copy of current hash state
* @returns Cloned hash instance
*/
clone(): T;
}import { sha256 } from '@noble/hashes/sha2.js';
function hashChunks(chunks: Uint8Array[]): Uint8Array {
const hasher = sha256.create();
for (const chunk of chunks) {
hasher.update(chunk);
}
return hasher.digest();
}import { sha256 } from '@noble/hashes/sha2.js';
function verifyHash(data: Uint8Array, expectedHash: Uint8Array): boolean {
const computed = sha256(data);
if (computed.length !== expectedHash.length) return false;
let diff = 0;
for (let i = 0; i < computed.length; i++) {
diff |= computed[i] ^ expectedHash[i];
}
return diff === 0;
}import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
const data = Uint8Array.from([0x10, 0x20, 0x30]);
// Compute multiple hashes
const hash256 = sha256(data); // 32 bytes
const hash384 = sha384(data); // 48 bytes
const hash512 = sha512(data); // 64 bytesimport { sha256 } from '@noble/hashes/sha2.js';
import { utf8ToBytes, bytesToHex } from '@noble/hashes/utils.js';
const message = "Hello, World!";
const hash = sha256(utf8ToBytes(message));
console.log(bytesToHex(hash));