CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-noble--hashes

Audited & minimal 0-dependency JS implementation of SHA, RIPEMD, BLAKE, HMAC, HKDF, PBKDF & Scrypt

Overview
Eval results
Files

sha2.mddocs/

SHA-2 Hashing

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.

Imports

import { sha256, sha384, sha512, sha224, sha512_224, sha512_256 } from '@noble/hashes/sha2.js';

Capabilities

SHA-256

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();

SHA-384

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>;

SHA-512

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>;

SHA-224

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>;

SHA-512/256

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>;

SHA-512/224

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>;

Common Hash Interface

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;
}

Usage Examples

File Hashing Pattern

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();
}

Hash Comparison

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;
}

Multiple Hash Variants

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 bytes

String Hashing

import { 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));

Technical Details

Algorithm Specifications

  • SHA-256/224: Defined in FIPS 180-4, uses 32-bit words, 64 rounds
  • SHA-512/384/512-256/512-224: Defined in FIPS 180-4, uses 64-bit words, 80 rounds
  • Truncated variants: Use modified initial values to prevent length extension attacks

Performance Characteristics

  • SHA-256 is faster on 32-bit platforms
  • SHA-512 variants are faster on 64-bit platforms
  • Incremental hashing has minimal overhead
  • Block-aligned inputs provide best performance

Security Notes

  • All SHA-2 variants are currently considered secure for cryptographic use
  • No practical pre-image or collision attacks known
  • SHA-256 provides 128-bit security against quantum attacks (use SHA-512 for 256-bit quantum security)
  • Recommended minimum: SHA-256 for general use, SHA-512 for long-term security

References

  • FIPS 180-4: Secure Hash Standard
  • RFC 4634: US Secure Hash Algorithms
  • Truncated SHA-512/256 paper: Security analysis

Install with Tessl CLI

npx tessl i tessl/npm-noble--hashes@2.0.0

docs

argon2.md

blake.md

eskdf.md

hkdf.md

hmac.md

index.md

legacy.md

pbkdf2.md

scrypt.md

sha2.md

sha3-addons.md

sha3.md

utils.md

webcrypto.md

tile.json