What is js-md5?
The js-md5 npm package is a lightweight JavaScript library used to generate MD5 hashes. It is commonly used for hashing strings, files, and other data types to ensure data integrity, create unique identifiers, and for cryptographic purposes.
What are js-md5's main functionalities?
Hashing a String
This feature allows you to generate an MD5 hash from a given string. The code sample demonstrates how to hash the string 'Hello, World!' and output the resulting hash.
const md5 = require('js-md5');
const hash = md5('Hello, World!');
console.log(hash); // Outputs: '65a8e27d8879283831b664bd8b7f0ad4'
Hashing a File
This feature allows you to generate an MD5 hash from a file. The code sample demonstrates how to read a file into a buffer and then hash its contents.
const md5 = require('js-md5');
const fs = require('fs');
const fileBuffer = fs.readFileSync('path/to/file.txt');
const hash = md5(fileBuffer);
console.log(hash);
Hashing an ArrayBuffer
This feature allows you to generate an MD5 hash from an ArrayBuffer. The code sample demonstrates how to create an ArrayBuffer, populate it with data, and then hash its contents.
const md5 = require('js-md5');
const buffer = new ArrayBuffer(8);
const view = new Uint8Array(buffer);
view[0] = 0x01;
view[1] = 0x02;
const hash = md5(buffer);
console.log(hash);
Other packages similar to js-md5
crypto-js
Crypto-js is a widely-used library that provides a variety of cryptographic algorithms, including MD5, SHA-1, SHA-256, and more. It is more comprehensive than js-md5, offering a broader range of cryptographic functionalities.
md5
The md5 package is another popular library for generating MD5 hashes. It is similar to js-md5 in terms of functionality but is often preferred for its simplicity and ease of use.
hash.js
Hash.js is a library that provides a variety of hash functions, including MD5, SHA-1, and SHA-256. It is known for its performance and flexibility, making it a good alternative to js-md5 for more complex hashing needs.
js-md5
A simple and fast MD5 hash function for JavaScript supports UTF-8 encoding.
Demo
MD5 Online
MD5 File Checksum Online
Download
Compress
Uncompress
Benchmark
jsPerf Benchmark
File Benchmark
Installation
You can also install js-md5 by using Bower.
bower install md5
For node.js, you can use this command to install:
npm install js-md5
Notice
buffer
method is deprecated. This maybe confuse with Buffer in node.js. Please use arrayBuffer
instead.
Usage
You could use like this:
md5('Message to hash');
var hash = md5.create();
hash.update('Message to hash');
hash.hex();
md5.hmac('key', 'Message to hash');
var hash = md5.hmac.create('key');
hash.update('Message to hash');
hash.hex();
Node.js
If you use node.js, you should require the module first:
var md5 = require('js-md5');
TypeScript
If you use TypeScript, you can import like this:
import { md5 } from 'js-md5';
RequireJS
It supports AMD:
require(['your/path/md5.js'], function(md5) {
});
See document
Example
md5('');
md5('The quick brown fox jumps over the lazy dog');
md5('The quick brown fox jumps over the lazy dog.');
md5('中文');
md5([]);
md5(new Uint8Array([]));
md5('');
md5.hex('');
md5.array('');
md5.digest('');
md5.arrayBuffer('');
md5.buffer('');
md5.base64('');
md5.hmac.hex('key', 'Message to hash');
md5.hmac.array('key', 'Message to hash');
License
The project is released under the MIT license.
Contact
The project's website is located at https://github.com/emn178/js-md5
Author: Chen, Yi-Cyuan (emn178@gmail.com)