jsSHA Documentation

repository·master·Indexed 24 days ago

https://github.com/caligatio/jssha

A pure TypeScript/JavaScript streaming implementation of the Secure Hash Standard (SHA) family. Supports SHA-1, SHA-2 (224, 256, 384, 512), SHA-3 (224, 256, 384, 512), SHAKE128/256, cSHAKE128/256, and KMAC128/256, along with HMAC support. Compatible with Browser, Node.js, and Deno environments.

Tokens
1.7K
Snippets
8
Records
9
Agent score
31%

What's inside jsSHA

  1. Install jsSHA

    master

    Depending on your environment, you can install or include jsSHA as follows:

    Browser

    Include the desired JavaScript file in your HTML header:

    <script type="text/javascript" src="/path/to/sha.js"></script>

    Node.js

    Install via npm:

    npm install jssha

    To use it, you can require the full module or specific variants (using subpath exports in Node.js v13+):

    const jsSHA = require("jssha");
    const jsSHA1 = require("jssha/sha1");
    
    // For older Node.js versions without subpath exports:
    const jsSHA1 = require("jssha/dist/sha1");
    
    // Using ESM (Node.js v13+):
    import jsSHA from "jssha";

    Deno

    Use the npm: import specifier:

    import jsSHA from "npm:jssha";
    import jsSHA1 from "npm:jssha/sha1";
    npm install jssha
  2. Configure input data formats and encodings

    master

    When providing input to jsSHA functions, you can specify the data format and encoding using the GenericInputType structure. This allows you to pass strings (with optional encoding), encoded strings (HEX, B64, BYTES), or raw binary data (ArrayBuffer or Uint8Array).

    Supported Encodings

    • UTF8
    • UTF16BE
    • UTF16LE

    Supported Formats

    • TEXT: For raw strings. Requires an optional encoding.
    • B64: Base64 encoded strings.
    • HEX: Hexadecimal strings.
    • BYTES: Byte strings.
    • ARRAYBUFFER: Raw ArrayBuffer objects.
    • UINT8ARRAY: Raw Uint8Array objects.
  3. Perform standard hashing

    master

    To calculate a hash, instantiate a jsSHA object with a hash variant and an input format. You can stream data into the object using the .update() method, which can be chained. Finally, call .getHash() to retrieve the result in your desired output format.

    Hash Variants: SHA-1, SHA-224, SHA3-224, SHA-256, SHA3-256, SHA-384, SHA3-384, SHA-512, SHA3-512, SHAKE128, or SHAKE256.

    Input Formats: HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY.

    Output Formats: B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY.

    Constructor Options:

    • encoding: (For TEXT inputs) "UTF8", "UTF16BE", or "UTF16LE". Defaults to "UTF8".
    • numRounds: Number of hashing iterations. Defaults to 1.

    getHash Options:

    • outputUpper: (For HEX output) Boolean. Defaults to false.
    • b64Pad: (For B64 output) Padding character. Defaults to "=".

    Important for SHAKE: SHAKE128 and SHAKE256 require an outputLen property in the getHash options object, representing the desired output length in multiples of 8 bits.

    const shaObj = new jsSHA("SHA-512", "TEXT", { encoding: "UTF8" });
    /* .update() can be chained */
    shaObj.update("This is").update(" a ");
    shaObj.update("test");
    const hash = shaObj.getHash("HEX");
  4. Calculate KMAC

    master

    To calculate KMAC, use the variants KMAC128 or KMAC256. The third argument accepts an options object containing an optional customization and a required kmacKey. Both require a value and a format.

    Important:

    • outputLen is required in the getHash options object (the desired output length in multiples of 8 bits).
    • You cannot specify numRounds with KMAC.

    Example:

    const shaObj = new jsSHA("KMAC128", "TEXT", {
      customization: { value: "My Tagged Application", format: "TEXT" },
      kmacKey: { value: "abc", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const kmac = shaObj.getHash("HEX", { outputLen: 256 });
    const shaObj = new jsSHA("KMAC128", "TEXT", {
      customization: { value: "My Tagged Application", format: "TEXT" },
      kmacKey: { value: "abc", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const kmac = shaObj.getHash("HEX", { outputLen: 256 });
  5. Calculate cSHAKE

    master

    To calculate cSHAKE, use the variants CSHAKE128 or CSHAKE256. The third argument accepts an options object containing optional customization and funcName objects. Both require a value and a format.

    Important:

    • outputLen is required in the getHash options object (the desired output length in multiples of 8 bits).
    • You cannot specify numRounds with cSHAKE.

    Example:

    const shaObj = new jsSHA("CSHAKE128", "TEXT", {
      customization: { value: "My Tagged Application", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const cshake = shaObj.getHash("HEX", { outputLen: 256 });
    const shaObj = new jsSHA("CSHAKE128", "TEXT", {
      customization: { value: "My Tagged Application", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const cshake = shaObj.getHash("HEX", { outputLen: 256 });
  6. Calculate HMAC

    master

    To calculate an HMAC, instantiate jsSHA with a hash variant and input format, then provide an hmacKey in the third argument. The key object requires a value (string, ArrayBuffer, or Uint8Array) and a format (matching the supported input formats).

    Note: You cannot specify numRounds when using HMAC.

    Example:

    const shaObj = new jsSHA("SHA-512", "TEXT", {
      hmacKey: { value: "abc", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const hmac = shaObj.getHash("HEX");
    const shaObj = new jsSHA("SHA-512", "TEXT", {
      hmacKey: { value: "abc", format: "TEXT" },
    });
    shaObj.update("This is a ");
    shaObj.update("test");
    const hmac = shaObj.getHash("HEX");
  7. Reference: SHAKE, cSHAKE, and KMAC options

    master

    jsSHA provides specific option interfaces for SHAKE, cSHAKE, and KMAC algorithms. Most interfaces support an optional encoding property to handle text-based inputs.

    // SHAKE Options
    export interface SHAKEOptionsNoEncodingType {
      numRounds?: number;
    }
    
    // cSHAKE Options
    export interface CSHAKEOptionsNoEncodingType {
      customization?: GenericInputType;
      funcName?: GenericInputType;
    }
    
    // KMAC Options
    export interface KMACOptionsNoEncodingType {
      kmacKey: GenericInputType;
      customization?: GenericInputType;
    }