Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
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.
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);
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.
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 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.
A simple MD5 hash function for JavaScript supports UTF-8 encoding.
You can also install js-md5 by using Bower.
bower install js-md5
For node.js, you can use this command to install:
npm install js-md5
You could use like this:
md5('Message to hash');
If you use node.js, you should require the module first:
md5 = require('js-md5');
Hash string to md5, set asciiOnly to true for better performace if you ensure input is ascii.
String
String to hash.
Boolean
(default: false
)Specify the string encoding is ASCII.
Code
md5('');
md5('The quick brown fox jumps over the lazy dog', true);
md5('The quick brown fox jumps over the lazy dog.', true);
Output
d41d8cd98f00b204e9800998ecf8427e
9e107d9d372bb6826bd81d3542a419d6
e4d909c290d0fb1ca068ffaddf22cbd0
It also supports UTF-8 encoding:
Code
md5('中文');
Output
a7bac2239fcdcb3a067903d8077c4a07
If you prefer jQuery style, you can add following code to add a jQuery extension.
Code
jQuery.md5 = md5
And then you could use like this:
$.md5('message');
If you prefer prototype style, you can add following code to add a prototype extension.
Code
String.prototype.md5 = function() {
return md5(this);
};
And then you could use like this:
'message'.md5();
The project is released under the MIT license.
The project's website is located at https://github.com/emn178/js-md5
Author: emn178@gmail.com
v0.1.3 / 2015-01-05
FAQs
A simple MD5 hash function for JavaScript supports UTF-8 encoding.
The npm package js-md5 receives a total of 95,199 weekly downloads. As such, js-md5 popularity was classified as popular.
We found that js-md5 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.