or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

hmac.mdindex.mdripemd.mdsha-hashes.mdutilities.md
tile.json

sha-hashes.mddocs/

SHA Hash Functions

Complete implementation of the SHA (Secure Hash Algorithm) family providing cryptographically secure hash functions with different output sizes and security levels.

Capabilities

SHA-1 Hash Function

SHA-1 cryptographic hash function producing 160-bit hash values. Note: SHA-1 is considered cryptographically broken and should not be used for security-critical applications.

/**
 * Creates a new SHA-1 hash instance
 * @returns SHA-1 hash instance
 */
function sha1(): SHA1Instance;

interface SHA1Instance extends BlockHash {
  blockSize: 512;
  outSize: 160;
  hmacStrength: 80;
  padLength: 64;
  endian: 'big';
}

Usage Examples:

const hash = require('hash.js');

// Basic SHA-1 hashing
const digest = hash.sha1().update('hello world').digest('hex');
console.log(digest); // "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"

// Incremental hashing
const hasher = hash.sha1();
hasher.update('hello');
hasher.update(' ');
hasher.update('world');
const result = hasher.digest('hex');

// Hex input
const hexDigest = hash.sha1().update('deadbeef', 'hex').digest('hex');

SHA-224 Hash Function

SHA-224 cryptographic hash function producing 224-bit hash values. It's a truncated version of SHA-256.

/**
 * Creates a new SHA-224 hash instance
 * @returns SHA-224 hash instance
 */
function sha224(): SHA224Instance;

interface SHA224Instance extends BlockHash {
  blockSize: 512;
  outSize: 224;
  hmacStrength: 192;
  padLength: 64;
  endian: 'big';
}

Usage Examples:

const hash = require('hash.js');

// Basic SHA-224 hashing
const digest = hash.sha224().update('hello world').digest('hex');
console.log(digest); // "2f05477fc24bb4faefd86c31a8b474b1a9e7a7fd62b4e3b87c99de5a3"

// Multiple updates
const hasher = hash.sha224();
hasher.update('part1');
hasher.update('part2');
const result = hasher.digest('hex');

SHA-256 Hash Function

SHA-256 cryptographic hash function producing 256-bit hash values. It's widely used and considered secure for current applications.

/**
 * Creates a new SHA-256 hash instance
 * @returns SHA-256 hash instance
 */
function sha256(): SHA256Instance;

interface SHA256Instance extends BlockHash {
  blockSize: 512;
  outSize: 256;
  hmacStrength: 192;
  padLength: 64;
  endian: 'big';
}

Usage Examples:

const hash = require('hash.js');

// Basic SHA-256 hashing
const digest = hash.sha256().update('hello world').digest('hex');
console.log(digest); // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"

// Binary output
const binaryDigest = hash.sha256().update('hello world').digest();
console.log(binaryDigest); // Array of bytes

// Selective import for smaller bundle
const sha256 = require('hash.js/lib/hash/sha/256');
const digest2 = sha256().update('test').digest('hex');

SHA-384 Hash Function

SHA-384 cryptographic hash function producing 384-bit hash values. It's a truncated version of SHA-512.

/**
 * Creates a new SHA-384 hash instance
 * @returns SHA-384 hash instance
 */
function sha384(): SHA384Instance;

interface SHA384Instance extends BlockHash {
  blockSize: 1024;
  outSize: 384;
  hmacStrength: 192;
  padLength: 128;
  endian: 'big';
}

Usage Examples:

const hash = require('hash.js');

// Basic SHA-384 hashing
const digest = hash.sha384().update('hello world').digest('hex');
console.log(digest); // "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd"

// Large data processing
const hasher = hash.sha384();
for (let i = 0; i < 1000; i++) {
  hasher.update(`chunk${i}`);
}
const result = hasher.digest('hex');

SHA-512 Hash Function

SHA-512 cryptographic hash function producing 512-bit hash values. It offers the highest security level in the SHA-2 family.

/**
 * Creates a new SHA-512 hash instance
 * @returns SHA-512 hash instance
 */
function sha512(): SHA512Instance;

interface SHA512Instance extends BlockHash {
  blockSize: 1024;
  outSize: 512;
  hmacStrength: 192;
  padLength: 128;
  endian: 'big';
}

Usage Examples:

const hash = require('hash.js');

// Basic SHA-512 hashing
const digest = hash.sha512().update('hello world').digest('hex');
console.log(digest); // "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"

// Processing binary data
const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
const binaryDigest = hash.sha512().update(binaryData).digest('hex');

Common Interface

All SHA hash functions share the same interface through the BlockHash base class:

interface BlockHash {
  /**
   * Update the hash with new data
   * @param msg - Data to hash (string, array, or Uint8Array)
   * @param enc - Encoding for string input ('hex' or undefined for UTF-8)
   * @returns this (for method chaining)
   */
  update(msg: any, enc?: 'hex'): this;
  
  /**
   * Generate the final hash digest
   * @param enc - Output encoding ('hex' for string, undefined for byte array)
   * @returns Hash digest as string or byte array
   */
  digest(): number[];
  digest(enc: 'hex'): string;
  
  // Static properties
  readonly blockSize: number;    // Block size in bits
  readonly outSize: number;      // Output size in bits
  readonly hmacStrength: number; // HMAC security strength
  readonly padLength: number;    // Padding length in bytes
  readonly endian: 'big' | 'little'; // Byte order
}

Static Properties

Each hash function exposes static properties for configuration and introspection:

// SHA-1
hash.sha1.blockSize === 512;
hash.sha1.outSize === 160;
hash.sha1.hmacStrength === 80;
hash.sha1.padLength === 64;

// SHA-256
hash.sha256.blockSize === 512;
hash.sha256.outSize === 256;
hash.sha256.hmacStrength === 192;
hash.sha256.padLength === 64;

// SHA-512
hash.sha512.blockSize === 1024;
hash.sha512.outSize === 512;
hash.sha512.hmacStrength === 192;
hash.sha512.padLength === 128;