or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-sha3.mdcore-hashing.mdhmac-operations.mdindex.mdvariant-classes.md
tile.json

tessl/npm-jssha

Pure TypeScript/JavaScript streaming implementation of the complete Secure Hash Standard (SHA) family with HMAC support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jssha@3.3.x

To install, run

npx @tessl/cli install tessl/npm-jssha@3.3.0

index.mddocs/

jsSHA

jsSHA provides a pure TypeScript/JavaScript streaming implementation of the complete Secure Hash Standard (SHA) family. It supports all SHA variants (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512, SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC functionality, streaming input processing, multiple input/output formats, and zero dependencies.

Package Information

  • Package Name: jssha
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jssha

Core Imports

import jsSHA from "jssha";

For CommonJS:

const jsSHA = require("jssha");

Submodule imports for specific SHA variants:

import jsSHA1 from "jssha/sha1";
import jsSHA256 from "jssha/sha256";
import jsSHA512 from "jssha/sha512";
import jsSHA3 from "jssha/sha3";

Basic Usage

import jsSHA from "jssha";

// Basic hashing
const shaObj = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" });
shaObj.update("This is a test");
const hash = shaObj.getHash("HEX");

// HMAC
const hmacObj = new jsSHA("SHA-256", "TEXT", {
  hmacKey: { value: "secret-key", format: "TEXT" }
});
hmacObj.update("This is a test");
const hmac = hmacObj.getHash("HEX");

// Variable length output (SHAKE)
const shakeObj = new jsSHA("SHAKE128", "TEXT");
shakeObj.update("This is a test");
const shakeHash = shakeObj.getHash("HEX", { outputLen: 256 });

Architecture

jsSHA is built around several key components:

  • Unified API: Single jsSHA class supporting all SHA variants through constructor parameters
  • Streaming Interface: update() method for processing data in chunks, supporting chainable calls
  • Variant-Specific Classes: Individual classes (jsSHA1, jsSHA256, jsSHA512, jsSHA3) for optimized builds
  • Type System: Complete TypeScript definitions with overloaded constructors for different variant/option combinations
  • Format Flexibility: Support for multiple input/output formats (TEXT, HEX, B64, BYTES, ARRAYBUFFER, UINT8ARRAY)
  • HMAC Integration: Built-in HMAC support for all applicable variants
  • Advanced Features: Support for cSHAKE customization, KMAC keyed hashing, and variable-length output

Capabilities

Core Hashing

Universal hashing functionality supporting all SHA variants with streaming input processing and multiple output formats.

class jsSHA {
  constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);
  constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);
  constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType);
  constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType);
  constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType);
  constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType);

  update(input: string | ArrayBuffer | Uint8Array): this;
  getHash(format: "HEX", options?: { outputUpper?: boolean; outputLen?: number; shakeLen?: number }): string;
  getHash(format: "B64", options?: { b64Pad?: string; outputLen?: number; shakeLen?: number }): string;
  getHash(format: "BYTES", options?: { outputLen?: number; shakeLen?: number }): string;
  getHash(format: "UINT8ARRAY", options?: { outputLen?: number; shakeLen?: number }): Uint8Array;
  getHash(format: "ARRAYBUFFER", options?: { outputLen?: number; shakeLen?: number }): ArrayBuffer;
}

type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512";
type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";

Core Hashing

HMAC Operations

Hash-based Message Authentication Code functionality for creating and verifying message authenticity.

interface FixedLengthOptionsEncodingType {
  hmacKey?: GenericInputType;
  encoding?: EncodingType;
} | {
  numRounds?: number;
  encoding?: EncodingType;
}

interface GenericInputType {
  value: string | ArrayBuffer | Uint8Array;
  format: "TEXT" | "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
  encoding?: EncodingType;
}

type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";

HMAC Operations

Advanced SHA3 Features

Advanced SHA3 functionality including SHAKE, cSHAKE, and KMAC variants with customization and variable-length output.

interface SHAKEOptionsEncodingType {
  numRounds?: number;
  encoding?: EncodingType;
}

interface CSHAKEOptionsEncodingType {
  customization?: GenericInputType;
  funcName?: GenericInputType;
  encoding?: EncodingType;
}

interface KMACOptionsEncodingType {
  kmacKey: GenericInputType;
  customization?: GenericInputType;
  encoding?: EncodingType;
}

Advanced SHA3 Features

Variant-Specific Classes

Optimized classes for specific SHA variants, useful for smaller bundle sizes when only specific variants are needed.

declare class jsSHA1 {
  constructor(variant: "SHA-1", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  constructor(variant: "SHA-1", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  update(input: string | ArrayBuffer | Uint8Array): this;
  getHash(format: "HEX" | "B64" | "BYTES" | "UINT8ARRAY" | "ARRAYBUFFER", options?: any): any;
  setHMACKey(key: any, inputFormat: any, options?: any): void;
  getHMAC(format: any, options?: any): any;
}

declare class jsSHA256 {
  constructor(variant: "SHA-224" | "SHA-256", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  constructor(variant: "SHA-224" | "SHA-256", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  // ... same methods as jsSHA1
}

declare class jsSHA512 {
  constructor(variant: "SHA-384" | "SHA-512", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  constructor(variant: "SHA-384" | "SHA-512", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  // ... same methods as jsSHA1
}

declare class jsSHA3 {
  constructor(variant: "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512", inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);
  constructor(variant: "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512", inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);
  constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);
  constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);
  constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: "TEXT", options?: CSHAKEOptionsEncodingType);
  constructor(variant: "CSHAKE128" | "CSHAKE256", inputFormat: FormatNoTextType, options?: CSHAKEOptionsNoEncodingType);
  constructor(variant: "KMAC128" | "KMAC256", inputFormat: "TEXT", options: KMACOptionsEncodingType);
  constructor(variant: "KMAC128" | "KMAC256", inputFormat: FormatNoTextType, options: KMACOptionsNoEncodingType);
  // ... same methods as jsSHA1
}

Variant-Specific Classes

Types

type EncodingType = "UTF8" | "UTF16BE" | "UTF16LE";
type FormatNoTextType = "HEX" | "B64" | "BYTES" | "ARRAYBUFFER" | "UINT8ARRAY";
type FormatType = "TEXT" | FormatNoTextType;

interface GenericInputType {
  value: string;
  format: "TEXT";
  encoding?: EncodingType;
} | {
  value: string;
  format: "B64" | "HEX" | "BYTES";
} | {
  value: ArrayBuffer;
  format: "ARRAYBUFFER";
} | {
  value: Uint8Array;
  format: "UINT8ARRAY";
}

interface packedValue {
  value: number[];
  binLen: number;
}

type FixedLengthVariantType = "SHA-1" | "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512" | "SHA3-224" | "SHA3-256" | "SHA3-384" | "SHA3-512";