or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-sha-js

Streamable SHA hashes in pure JavaScript

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/sha.js@2.4.x

To install, run

npx @tessl/cli install tessl/npm-sha-js@2.4.0

index.mddocs/

sha.js

sha.js provides pure JavaScript implementations of SHA (Secure Hash Algorithm) functions including SHA-0, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. It offers a Node.js-compatible API for creating streamable hash objects that can process data incrementally, making it suitable for hashing large amounts of data without loading everything into memory.

Package Information

  • Package Name: sha.js
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install sha.js

Core Imports

const shajs = require('sha.js');
// Or import specific algorithms
const { sha256, sha1, sha512 } = require('sha.js');

For ES modules:

import shajs from 'sha.js';
// Or import specific algorithms
import { sha256, sha1, sha512 } from 'sha.js';

Basic Usage

const shajs = require('sha.js');

// Using factory function
const hash = shajs('sha256').update('hello world').digest('hex');
console.log(hash); // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"

// Using direct constructor
const sha256Hash = new shajs.sha256().update('hello world').digest('hex');

// Streaming usage
const stream = shajs('sha256');
stream.update('hello ');
stream.update('world');
const result = stream.digest('hex');

Architecture

sha.js is built around several key components:

  • Factory Function: Main export that creates hash instances based on algorithm name
  • Hash Base Class: Common functionality for update(), digest(), and incremental processing
  • Algorithm Implementations: Specific SHA variant classes (Sha, Sha1, Sha224, Sha256, Sha384, Sha512)
  • Inheritance Pattern: All algorithms inherit from base Hash class using 'inherits' module
  • Incremental Processing: Supports streaming/chunked data processing without memory limitations

Capabilities

Hash Factory Function

Creates hash instances for any supported SHA algorithm.

/**
 * Creates a SHA hash instance for the specified algorithm
 * @param {string} algorithm - Algorithm name (case-insensitive): 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'
 * @returns {Hash} Hash instance for the specified algorithm
 * @throws {Error} If algorithm is not supported
 */
function shajs(algorithm);

Usage Example:

// Create different hash types
const sha1Hash = shajs('sha1');
const sha256Hash = shajs('sha256');
const sha512Hash = shajs('SHA512'); // Case insensitive

Algorithm Constructors

Direct access to specific SHA algorithm constructors.

/**
 * SHA-0 hash constructor (legacy, deprecated)
 */
function Sha();

/**
 * SHA-1 hash constructor (legacy, deprecated for security)
 */
function Sha1();

/**
 * SHA-224 hash constructor
 */
function Sha224();

/**
 * SHA-256 hash constructor
 */
function Sha256();

/**
 * SHA-384 hash constructor
 */
function Sha384();

/**
 * SHA-512 hash constructor
 */
function Sha512();

Usage Example:

const { sha256, sha1 } = require('sha.js');

// Direct instantiation
const hash1 = new sha256();
const hash2 = new sha1();

Hash Instance Methods

All hash instances provide the same interface for incremental data processing.

/**
 * Updates the hash with new data
 * @param {string|Buffer} data - Data to add to hash
 * @param {string} [encoding='utf8'] - Encoding for string data
 * @returns {Hash} this (for method chaining)
 */
update(data, encoding);

/**
 * Finalizes the hash computation and returns the result
 * @param {string} [encoding] - Output encoding ('hex', 'base64', etc.)
 * @returns {Buffer|string} Hash result as Buffer (no encoding) or string (with encoding)
 */
digest(encoding);

/**
 * Initializes/resets the hash state to start fresh
 * @returns {Hash} this
 */
init();

Usage Examples:

const shajs = require('sha.js');

// Method chaining
const result = shajs('sha256')
  .update('hello')
  .update(' ')
  .update('world')
  .digest('hex');

// Step by step
const hash = shajs('sha256');
hash.update('data chunk 1');
hash.update('data chunk 2');
const hexResult = hash.digest('hex');
const bufferResult = hash.digest(); // Returns Buffer

// Reset and reuse
hash.init();
hash.update('new data');
const newResult = hash.digest('hex');

Command Line Interface

sha.js includes a command-line tool for hashing files or stdin.

# Hash a file with SHA-256
sha.js sha256 filename.txt

# Hash stdin with SHA-1 (default)
echo "hello world" | sha.js

# Hash stdin with specific algorithm
echo "hello world" | sha.js sha512

# Show help
sha.js --help

Supported Algorithms

// Available algorithm names (case-insensitive)
type SupportedAlgorithm = 'sha' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';
  • sha / sha-0: SHA-0 (legacy, deprecated for security reasons)
  • sha1 / sha-1: SHA-1 (legacy, deprecated for security reasons)
  • sha224 / sha-224: SHA-224 (28-byte output)
  • sha256 / sha-256: SHA-256 (32-byte output)
  • sha384 / sha-384: SHA-384 (48-byte output)
  • sha512 / sha-512: SHA-512 (64-byte output)

Error Handling

const shajs = require('sha.js');

try {
  const hash = shajs('unsupported-algorithm');
} catch (error) {
  console.log(error.message); // "unsupported-algorithm is not supported (we accept pull requests)"
}

Types

/**
 * Base hash class that all algorithm implementations extend
 */
interface Hash {
  /** Update hash with new data */
  update(data: string | Buffer, encoding?: string): Hash;
  /** Finalize and return hash result */
  digest(encoding?: string): Buffer | string;
  /** Reset hash state */
  init(): Hash;
}

/**
 * SHA-0 hash implementation (legacy)
 */
interface Sha extends Hash {}

/**
 * SHA-1 hash implementation (legacy)
 */
interface Sha1 extends Hash {}

/**
 * SHA-224 hash implementation
 */
interface Sha224 extends Hash {}

/**
 * SHA-256 hash implementation
 */
interface Sha256 extends Hash {}

/**
 * SHA-384 hash implementation
 */
interface Sha384 extends Hash {}

/**
 * SHA-512 hash implementation
 */
interface Sha512 extends Hash {}