or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-crypto-hash

Tiny hashing module that uses the native crypto API in Node.js and the browser

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/crypto-hash@3.1.x

To install, run

npx @tessl/cli install tessl/npm-crypto-hash@3.1.0

index.mddocs/

Crypto Hash

Crypto Hash is a tiny isomorphic hashing library that provides a unified API for cryptographic hashing across Node.js and browser environments. It implements SHA-1, SHA-256, SHA-384, and SHA-512 algorithms using native crypto APIs for optimal performance and security.

Package Information

  • Package Name: crypto-hash
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install crypto-hash

Core Imports

import { sha256, sha512, sha1, sha384 } from "crypto-hash";

Environment-specific imports (advanced):

// Force Node.js implementation
import { sha256 } from "crypto-hash/index.js";

// Force browser implementation  
import { sha256 } from "crypto-hash/browser.js";

Basic Usage

import { sha256 } from "crypto-hash";

// Hash a string
const hash = await sha256("πŸ¦„");
console.log(hash);
// => '36bf255468003165652fe978eaaa8898e191664028475f83f506dabd95298efc'

// Hash binary data with different output format
const buffer = new TextEncoder().encode("Hello World");
const hashBuffer = await sha256(buffer, { outputFormat: "buffer" });

Architecture

Crypto Hash provides platform-specific optimizations while maintaining a consistent API:

  • Node.js Implementation: Uses node:crypto with worker thread optimization for non-blocking operations
  • Browser Implementation: Uses Web Crypto API (crypto.subtle.digest) with minimal bundle size (~300 bytes minified & gzipped)
  • Automatic Detection: Conditionally loads the appropriate implementation based on the environment
  • Worker Thread Pool: In Node.js, operations are executed in worker threads that are lazily spawned and unref'd

Capabilities

SHA-1 Hashing

Computes SHA-1 hash of input data. Warning: SHA-1 is cryptographically insecure and should not be used for sensitive applications.

function sha1(
  input: string | ArrayBuffer | ArrayBufferView,
  options?: OptionsHexOutput
): Promise<string>;

function sha1(
  input: string | ArrayBuffer | ArrayBufferView,
  options: OptionBufferOutput
): Promise<ArrayBuffer>;

Usage Example:

import { sha1 } from "crypto-hash";

const hash = await sha1("πŸ¦„");
console.log(hash);
// => '5df82936cbf0864be4b7ba801bee392457fde9e4'

// Get ArrayBuffer output
const buffer = await sha1("data", { outputFormat: "buffer" });

SHA-256 Hashing

Computes SHA-256 hash of input data using industry-standard secure algorithm.

function sha256(
  input: string | ArrayBuffer | ArrayBufferView,
  options?: OptionsHexOutput
): Promise<string>;

function sha256(
  input: string | ArrayBuffer | ArrayBufferView,
  options: OptionBufferOutput
): Promise<ArrayBuffer>;

Usage Example:

import { sha256 } from "crypto-hash";

// String input
const hash = await sha256("πŸ¦„");
console.log(hash);
// => '36bf255468003165652fe978eaaa8898e191664028475f83f506dabd95298efc'

// ArrayBuffer input
const data = new Uint8Array([1, 2, 3, 4, 5]);
const bufferHash = await sha256(data);

SHA-384 Hashing

Computes SHA-384 hash of input data for applications requiring longer hash values.

function sha384(
  input: string | ArrayBuffer | ArrayBufferView,
  options?: OptionsHexOutput
): Promise<string>;

function sha384(
  input: string | ArrayBuffer | ArrayBufferView,
  options: OptionBufferOutput
): Promise<ArrayBuffer>;

Usage Example:

import { sha384 } from "crypto-hash";

const hash = await sha384("πŸ¦„");
console.log(hash);
// => 'a9d4dfb503394bd9701d60eb5fb1d7fb800580b43d874165103b16d311fb5c97545cb89f06c31f30e219f5b603e834ca'

SHA-512 Hashing

Computes SHA-512 hash of input data providing the highest level of hash security in the SHA-2 family.

function sha512(
  input: string | ArrayBuffer | ArrayBufferView,
  options?: OptionsHexOutput
): Promise<string>;

function sha512(
  input: string | ArrayBuffer | ArrayBufferView,
  options: OptionBufferOutput
): Promise<ArrayBuffer>;

Usage Example:

import { sha512 } from "crypto-hash";

const hash = await sha512("πŸ¦„");
console.log(hash);
// => '7d9e515c59bd15d0692f9bc0c68f50f82b62a99bef4b8dc490cec165296210dff005529a4cb84a655eee6ddec82339e6bdbab21bdb287b71a543a56cfab53905'

Types

type OptionsHexOutput = {
  outputFormat?: 'hex';
};

type OptionBufferOutput = {
  outputFormat: 'buffer';
};

Input Types:

  • string - UTF-8 encoded strings
  • ArrayBuffer - Raw binary data buffers
  • ArrayBufferView - Typed arrays like Uint8Array, Int32Array, etc.

Output Formats:

  • 'hex' (default) - Returns lowercase hexadecimal string representation
  • 'buffer' - Returns ArrayBuffer with raw binary hash data

Platform-Specific Behavior

Node.js Environment

  • Uses native node:crypto.createHash() for hash computation
  • Automatically offloads operations to worker threads for non-blocking execution
  • Worker threads are lazily created and automatically unref'd to avoid keeping the process alive
  • Requires Node.js version 18 or higher

Browser Environment

  • Uses Web Crypto API (crypto.subtle.digest) for native performance
  • Requires secure context (HTTPS) for crypto.subtle access
  • Ultra-lightweight implementation (~300 bytes minified & gzipped)
  • Compatible with modern browsers (Internet Explorer not supported)

Error Handling

Common error scenarios:

  • Secure Context Required: Browser environment without HTTPS will throw when accessing crypto.subtle
  • Invalid Input Types: Non-string, non-ArrayBuffer, non-ArrayBufferView inputs will cause runtime errors
  • Worker Thread Errors: In Node.js, worker thread failures are equivalent to segfaults and will throw at callback level
  • Unsupported Environment: Environments without crypto support will fail during module import