Skip to content

eustatos/pure-md5

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

74 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

pure-md5 🎯

A lightweight, zero-dependency JavaScript library for MD5 hashing with streaming support for large files.

npm version npm downloads License: MIT Build Status codecov Bundle Size TypeScript


πŸš€ Quick Start

Install

npm install pure-md5
# or
yarn add pure-md5
# or
pnpm add pure-md5

Basic Usage

import { md5 } from 'pure-md5/md5';

const hash = md5('hello');
console.log(hash); // "5d41402abc4b2a76b9719d911017c592"

Streaming (Large Files)

import { createMD5Stream } from 'pure-md5';
import fs from 'fs';

const stream = createMD5Stream();
stream.on('md5', result => console.log('MD5:', result.digest));

fs.createReadStream('large-file.bin').pipe(stream);

✨ Features

  • ⚑ Zero Dependencies - No external dependencies, ever
  • πŸ“¦ Tiny Bundle - ~1.4KB gzipped for md5() only (with tree-shaking), ~6KB for full bundle
  • 🎯 Multiple APIs - Simple, streaming, and promise-based
  • 🦺 TypeScript Ready - Full type definitions included
  • πŸ”Œ Adapter System - Automatic detection (WebCrypto, Node.js, Pure JS)
  • πŸ“„ File Hashing - Stream large files with progress tracking
  • 🌐 Universal - Works in Node.js and browsers

πŸ“š Documentation


πŸ› οΈ API Reference

Basic MD5

md5(message[, encoding])

Compute MD5 hash of a string or buffer.

import { md5 } from 'pure-md5/md5'; // Tree-shakeable, ~1.4KB

const hash = md5('hello');
console.log(hash); // "5d41402abc4b2a76b9719d911017c592"

Streaming API

createMD5Stream()

Create a new MD5Stream instance.

import { createMD5Stream } from 'pure-md5';

const stream = createMD5Stream();
stream.on('md5', result => {
  console.log('MD5:', result.digest);      // "5d41402abc4b2a76b9719d911017c592"
  console.log('Bytes:', result.bytesProcessed); // 5
});

pipeThroughMD5(source)

Pipe a stream through MD5 hashing and get a promise.

import { pipeThroughMD5, fromReadable } from 'pure-md5';

const source = fromReadable(['hello', ' ', 'world']);
const result = await pipeThroughMD5(source);
console.log('MD5:', result.digest);

fromStream(stream)

Convenience method to create a stream and get result.

import { fromStream } from 'pure-md5';
import fs from 'fs';

const { stream, result } = fromStream(fs.createReadStream('file.txt'));
result.then(r => console.log('MD5:', r.digest));

File System Utilities

hashFile(filePath, options?)

Hash a file asynchronously.

import { hashFile } from 'pure-md5';

const result = await hashFile('path/to/file.txt');
console.log('MD5:', result.digest);
console.log('Bytes:', result.bytesProcessed);

// With progress tracking
const progress = createProgressTracker(result.bytesProcessed, percent => {
  console.log(`Progress: ${percent.toFixed(1)}%`);
});

const result = await hashFile('large-file.bin', { onProgress: progress });

hashFileDigest(filePath)

Hash a file and return only the digest.

import { hashFileDigest } from 'pure-md5';

const digest = await hashFileDigest('path/to/file.txt');
console.log('MD5:', digest);

hashFileSync(filePath)

Hash a file synchronously (for small files).

import { hashFileSync } from 'pure-md5';

const digest = hashFileSync('small-file.txt');
console.log('MD5:', digest);

verifyFile(filePath, expectedDigest)

Verify file integrity using MD5.

import { verifyFile } from 'pure-md5';

const isVerified = await verifyFile('path/to/file.txt', '5d41402abc4b2a76b9719d911017c592');
console.log('Verified:', isVerified); // true or false

CDN Usage

<script src="https://unpkg.com/pure-md5@latest/dist/index.js"></script>
<script>
  console.log(md5('hello')); // "5d41402abc4b2a76b9719d911017c592"
</script>

πŸ“Š Comparison with Alternatives

Feature pure-md5 pvorb/node-md5 crypto-js js-md4 Node.js crypto
Bundle Size (md5 only) ~1.4KBΒΉ ~3KB ~4KB ~2KB N/A
Bundle Size (full) ~6KBΒΉ ~3KB ~4KB ~2KB N/A
Dependencies 0 0 0 0 0
Streaming βœ… ❌ ❌ ❌ βœ…
Browser Support βœ… ❌ βœ… βœ… ❌
TypeScript βœ… ❌ ❌ ⚠️ ❌
Zero Config βœ… ❌² βœ… ❌ βœ…
Tree-shaking βœ… ❌ ❌ ❌ N/A
Pure JS (no Node) βœ… ❌ βœ… βœ… ❌

ΒΉ With tree-shaking: Only import what you use!
Β² Requires Node.js environment - not browser-compatible

Why choose pure-md5 over pvorb/node-md5?

  1. Browser Support: pvorb/node-md5 only works in Node.js, while pure-md5 works everywhere
  2. Tree-shaking: pure-md5 supports modern tree-shaking for smaller bundles
  3. Streaming: built-in streaming API for large files in pure-md5
  4. TypeScript: first-class TypeScript support with full type definitions
  5. Zero dependencies: truly zero-dependency implementation
  6. Modern API: cleaner, more intuitive interface with promise-based options

pvorb/node-md5 is still a good choice if you only need Node.js and prefer its API style.


πŸ”§ Configuration

Node.js

// Node.js adapter is auto-detected
import { md5 } from 'pure-md5';

Browser

// WebCrypto adapter is auto-detected
import { md5 } from 'pure-md5';

Manual Adapter Selection

Use adapter backends directly when you need explicit control:

import { NodeCryptoBackend, WebCryptoBackend, PureJSBackend } from 'pure-md5';

// Create backend instances
const nodeBackend = new NodeCryptoBackend();
const hash = await nodeBackend.hash('hello');
console.log(hash);

🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details.

Setup

git clone https://github.com/eustatos/pure-md5.git
cd pure-md5
npm install
npm test

Running Tests

npm test                  # Run all tests
npm run coverage          # Generate coverage report
npm run build:watch       # Build in watch mode

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE.md file for details.


πŸ’™ Support This Project

If you find this project helpful, please consider supporting it:

  • ⭐ Star this repository
  • 🐦 Tweet about it
  • πŸ’¬ Share with your community
  • 🍺 Buy me a coffee (coming soon)

πŸ“š Related Resources


*Made with ❀️ by Aleksandr Astashkin

About

A lightweight JavaScript function for hashing messages by the MD5 algorithm

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages