TypeScript MD5 Hashing Library

2.0.1 · active · verified Sun Apr 19

ts-md5 is a TypeScript-first implementation of the MD5 hashing algorithm, designed for both Node.js (>=18) and browser environments. It provides functionalities for hashing Unicode and ASCII strings, supporting incremental hashing, and directly processing Files and Blobs. The library also includes advanced features like a `ParallelHasher` that leverages web workers for asynchronous file/blob hashing, improving performance in browser contexts. Its current stable version is 2.0.1, with releases showing active maintenance, including a recent major update to v2.0.0 in July 2025 that shifted to Vite for bundling. Key differentiators include its strong TypeScript typing, robust handling of various input types (strings, byte arrays, files), and built-in web worker support for computationally intensive tasks, making it a versatile choice for applications requiring MD5 checksums. It builds upon established MD5 implementations by Joseph Myers, André Cruz (SparkMD5), and Raymond Hill (yamd5.js).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic synchronous string hashing, incremental hashing with the Md5 class, and asynchronous blob hashing using ParallelHasher with a mocked Blob and worker path.

import { Md5, ParallelHasher } from 'ts-md5';

// Basic synchronous hashing
console.log('--- Basic Hashing ---');
const hexHash1 = Md5.hashStr('hello world');
console.log(`'hello world' (hex): ${hexHash1}`);

const rawHash1 = Md5.hashStr('hello world', true);
console.log(`'hello world' (raw Int32Array): ${rawHash1}`);

const asciiHash = Md5.hashAsciiStr('simple ascii');
console.log(`'simple ascii' (ascii hex): ${asciiHash}`);

// Incremental hashing
console.log('\n--- Incremental Hashing ---');
const md5Instance = new Md5();
md5Instance
    .appendStr('first part of the string ')
    .appendAsciiStr('second part')
    .appendStr(' and a final piece');
const incrementalHash = md5Instance.end();
console.log(`Incremental hash: ${incrementalHash}`);

// Example for hashing a Blob (requires a Blob instance, mocking for quickstart)
// In a real scenario, 'fileBlob' would come from an <input type="file"> event or similar.
async function hashBlobExample() {
    console.log('\n--- Hashing a Blob (ParallelHasher) ---');
    if (typeof Worker === 'undefined') {
        console.warn('Web Workers not available in this environment. Skipping ParallelHasher example.');
        return;
    }

    // Mocking a Blob
    const mockBlob = new Blob(['This is some content for the blob to be hashed.'], { type: 'text/plain' });
    
    // NOTE: Replace '/path/to/ts-md5/dist/md5_worker.js' with the actual URL where your worker script is served.
    // This typically means configuring your build tool (e.g., Vite, Webpack) to copy this file to your public directory.
    const workerPath = '/ts-md5/dist/md5_worker.js'; // Example relative path in a public folder
    let hasher: ParallelHasher | undefined;
    try {
        hasher = new ParallelHasher(workerPath);
        const result = await hasher.hash(mockBlob);
        console.log(`MD5 of mockBlob is: ${result}`);
    } catch (error) {
        console.error(`Error hashing blob with ParallelHasher: ${error}. Make sure '${workerPath}' is accessible.`);
    } finally {
        // Important: Terminate the worker to release resources
        hasher?.terminate();
    }
}

hashBlobExample();

view raw JSON →