CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jssha

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

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

core-hashing.mddocs/

Core Hashing

Core hashing functionality providing streaming input processing and hash generation for all SHA variants. The jsSHA class serves as the universal interface supporting traditional SHA variants (SHA-1, SHA-2 family) and modern SHA-3 variants.

Capabilities

jsSHA Constructor

Creates a new hashing instance for the specified SHA variant and input format.

/**
 * Creates a new jsSHA instance for fixed-length SHA variants with TEXT input
 * @param variant - SHA variant: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512
 * @param inputFormat - Must be "TEXT" for this overload
 * @param options - Optional configuration including encoding and HMAC key
 */
constructor(variant: FixedLengthVariantType, inputFormat: "TEXT", options?: FixedLengthOptionsEncodingType);

/**
 * Creates a new jsSHA instance for fixed-length SHA variants with binary input formats
 * @param variant - SHA variant: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512
 * @param inputFormat - Input format: HEX, B64, BYTES, ARRAYBUFFER, UINT8ARRAY
 * @param options - Optional configuration including HMAC key and numRounds
 */
constructor(variant: FixedLengthVariantType, inputFormat: FormatNoTextType, options?: FixedLengthOptionsNoEncodingType);

/**
 * Creates a new jsSHA instance for SHAKE variants with TEXT input
 * @param variant - SHAKE variant: SHAKE128 or SHAKE256
 * @param inputFormat - Must be "TEXT" for this overload
 * @param options - Optional configuration including encoding and numRounds
 */
constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: "TEXT", options?: SHAKEOptionsEncodingType);

/**
 * Creates a new jsSHA instance for SHAKE variants with binary input formats
 * @param variant - SHAKE variant: SHAKE128 or SHAKE256
 * @param inputFormat - Input format: HEX, B64, BYTES, ARRAYBUFFER, UINT8ARRAY
 * @param options - Optional configuration including numRounds
 */
constructor(variant: "SHAKE128" | "SHAKE256", inputFormat: FormatNoTextType, options?: SHAKEOptionsNoEncodingType);

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

Usage Examples:

import jsSHA from "jssha";

// Basic SHA-256 with text input
const sha256 = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" });

// SHA-1 with hex input
const sha1 = new jsSHA("SHA-1", "HEX");

// SHA3-512 with ArrayBuffer input
const sha3 = new jsSHA("SHA3-512", "ARRAYBUFFER");

// SHAKE128 for variable-length output
const shake = new jsSHA("SHAKE128", "TEXT");

update Method

Streams input data for hashing. Can be called multiple times to process data in chunks.

/**
 * Takes input and hashes as many blocks as possible. Stores the rest for future update or getHash calls.
 * @param input - The input to be hashed (format must match constructor inputFormat)
 * @returns Reference to the same object for method chaining
 */
update(input: string | ArrayBuffer | Uint8Array): this;

Usage Examples:

import jsSHA from "jssha";

const shaObj = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" });

// Single update
shaObj.update("Hello, World!");

// Multiple updates (streaming)
shaObj.update("Hello, ");
shaObj.update("World!");

// Chainable updates
shaObj.update("First part").update("Second part").update("Third part");

// Different input types based on constructor format
const hexSha = new jsSHA("SHA-256", "HEX");
hexSha.update("48656c6c6f"); // Hex string

const bufferSha = new jsSHA("SHA-256", "ARRAYBUFFER");
const buffer = new TextEncoder().encode("Hello").buffer;
bufferSha.update(buffer);

getHash Method

Returns the computed hash in the specified output format.

/**
 * Returns the hash in hexadecimal format
 * @param format - Must be "HEX"
 * @param options - Optional formatting options
 * @returns Hash as hexadecimal string
 */
getHash(format: "HEX", options?: { outputUpper?: boolean; outputLen?: number; shakeLen?: number }): string;

/**
 * Returns the hash in Base64 format
 * @param format - Must be "B64"
 * @param options - Optional formatting options
 * @returns Hash as Base64 string
 */
getHash(format: "B64", options?: { b64Pad?: string; outputLen?: number; shakeLen?: number }): string;

/**
 * Returns the hash as a byte string
 * @param format - Must be "BYTES"
 * @param options - Optional length options for variable-length variants
 * @returns Hash as byte string
 */
getHash(format: "BYTES", options?: { outputLen?: number; shakeLen?: number }): string;

/**
 * Returns the hash as a Uint8Array
 * @param format - Must be "UINT8ARRAY"
 * @param options - Optional length options for variable-length variants
 * @returns Hash as Uint8Array
 */
getHash(format: "UINT8ARRAY", options?: { outputLen?: number; shakeLen?: number }): Uint8Array;

/**
 * Returns the hash as an ArrayBuffer
 * @param format - Must be "ARRAYBUFFER"
 * @param options - Optional length options for variable-length variants
 * @returns Hash as ArrayBuffer
 */
getHash(format: "ARRAYBUFFER", options?: { outputLen?: number; shakeLen?: number }): ArrayBuffer;

Usage Examples:

import jsSHA from "jssha";

const shaObj = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" });
shaObj.update("Hello, World!");

// Different output formats
const hexHash = shaObj.getHash("HEX"); // "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"
const hexUpper = shaObj.getHash("HEX", { outputUpper: true }); // Uppercase hex
const b64Hash = shaObj.getHash("B64"); // Base64 encoded
const bytesHash = shaObj.getHash("BYTES"); // Byte string
const uint8Hash = shaObj.getHash("UINT8ARRAY"); // Uint8Array
const bufferHash = shaObj.getHash("ARRAYBUFFER"); // ArrayBuffer

// Variable-length output (SHAKE variants only)
const shakeObj = new jsSHA("SHAKE128", "TEXT");
shakeObj.update("Hello, World!");
const shake256bits = shakeObj.getHash("HEX", { outputLen: 256 }); // 256-bit output
const shake512bits = shakeObj.getHash("HEX", { outputLen: 512 }); // 512-bit output

Supported Hash Variants

Traditional SHA Variants

  • SHA-1: 160-bit output, legacy algorithm (deprecated for security-critical applications)
  • SHA-224: 224-bit output, part of SHA-2 family
  • SHA-256: 256-bit output, most commonly used SHA-2 variant
  • SHA-384: 384-bit output, part of SHA-2 family
  • SHA-512: 512-bit output, strongest SHA-2 variant

SHA-3 Variants

  • SHA3-224: 224-bit output using Keccak sponge function
  • SHA3-256: 256-bit output using Keccak sponge function
  • SHA3-384: 384-bit output using Keccak sponge function
  • SHA3-512: 512-bit output using Keccak sponge function

Variable-Length Variants

  • SHAKE128: Variable-length output based on 128-bit security level
  • SHAKE256: Variable-length output based on 256-bit security level

Note: Variable-length variants (SHAKE) require the outputLen parameter in getHash() options to specify the desired output length in bits.

Input Formats

  • TEXT: String input with encoding options (UTF8, UTF16BE, UTF16LE)
  • HEX: Hexadecimal string representation
  • B64: Base64 encoded string
  • BYTES: Raw byte string
  • ARRAYBUFFER: JavaScript ArrayBuffer object
  • UINT8ARRAY: JavaScript Uint8Array object

Output Formats

  • HEX: Hexadecimal string (case configurable)
  • B64: Base64 string (padding configurable)
  • BYTES: Raw byte string
  • ARRAYBUFFER: JavaScript ArrayBuffer object
  • UINT8ARRAY: JavaScript Uint8Array object

Options Configuration

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

interface FixedLengthOptionsNoEncodingType {
  hmacKey?: GenericInputType;
} | {
  numRounds?: number;
}

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

interface SHAKEOptionsNoEncodingType {
  numRounds?: number;
}

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

Configuration Options:

  • encoding: Text encoding for TEXT input format (defaults to UTF8)
  • numRounds: Number of hashing iterations (defaults to 1, not valid for MAC variants)
  • hmacKey: HMAC key specification for authenticated hashing

Error Handling

The jsSHA constructor will throw an Error if an unsupported variant is specified. All variants are case-sensitive and must match exactly as specified in the type definitions.

// This will throw an error
try {
  const invalid = new jsSHA("sha-256", "TEXT"); // Wrong case
} catch (error) {
  console.error("Invalid SHA variant specified");
}

// Correct usage
const valid = new jsSHA("SHA-256", "TEXT");

docs

advanced-sha3.md

core-hashing.md

hmac-operations.md

index.md

variant-classes.md

tile.json