What is blueimp-md5?
The blueimp-md5 package is a JavaScript function for hashing messages with MD5. It is commonly used to create 128-bit hash values from strings. The package can be used both in browser environments and in Node.js applications, making it versatile for various web development needs.
What are blueimp-md5's main functionalities?
MD5 Hashing
This feature allows the user to generate an MD5 hash from a given string. It is useful for creating unique identifiers or checksums from large volumes of data.
const md5 = require('blueimp-md5');
const hash = md5('Hello World');
console.log(hash); // Outputs MD5 hash of 'Hello World'
MD5 Hashing with a Key
This feature enhances the basic hashing functionality by allowing a key (or salt) to be included in the hash process. This is particularly useful for creating more secure hashes by adding an additional layer of complexity.
const md5 = require('blueimp-md5');
const hash = md5('Hello World', 'secretKey');
console.log(hash); // Outputs MD5 hash of 'Hello World' using 'secretKey' as the salt
MD5 Hashing with Raw Output
This feature allows the user to obtain the raw binary output of the MD5 hash, rather than the default hexadecimal string. This might be useful in scenarios where binary data is required for further processing.
const md5 = require('blueimp-md5');
const hash = md5('Hello World', null, true);
console.log(hash); // Outputs raw binary format of MD5 hash
Other packages similar to blueimp-md5
crypto-js
Crypto-js is a collection of cryptography algorithms implemented in JavaScript. Like blueimp-md5, it supports MD5 hashing, but it also includes a wider range of cryptographic functions such as SHA-1, SHA-256, and AES. This makes crypto-js more versatile if you need more than just MD5 hashing.
md5
The md5 package is another simple implementation of the MD5 hashing algorithm. It is very similar to blueimp-md5 in functionality and usage. However, blueimp-md5 offers additional features like key-based hashing and raw output, which are not available in the md5 package.
JavaScript MD5
Demo
JavaScript MD5 Demo
Description
JavaScript MD5 implementation. Compatible with server-side environments like
Node.js, module loaders like RequireJS, Browserify or webpack and all web
browsers.
Usage
Client-side
Include the (minified) JavaScript MD5
script in your HTML markup:
<script src="js/md5.min.js"></script>
In your application code, calculate the
(hex-encoded)
MD5 hash of a string by calling the md5
method with the string as argument:
var hash = md5("value");
Server-side
The following is an example how to use the JavaScript MD5 module on the
server-side with Node.js.
Create a new directory and add the md5.js file. Or alternatively,
install the blueimp-md5 package with npm:
npm install blueimp-md5
Add a file server.js with the following content:
require("http").createServer(function (req, res) {
var md5 = require("./md5"),
url = require("url"),
query = url.parse(req.url).query;
res.writeHead(200, {"Content-Type": "text/plain"});
res.end(md5(query));
}).listen(8080, "localhost");
console.log("Server running at http://localhost:8080/");
Run the application with the following command:
node server.js
Requirements
The JavaScript MD5 script has zero dependencies.
API
Calculate the (hex-encoded)
MD5 hash of a given string value:
var hash = md5("value");
Calculate the (hex-encoded)
HMAC-MD5 hash of a given string value and key:
var hash = md5("value", "key");
Calculate the raw MD5 hash of a given
string value:
var hash = md5("value", null, true);
Calculate the raw HMAC-MD5 hash of a given
string value and key:
var hash = md5("value", "key", true);
Tests
The JavaScript MD5 project comes with
Unit Tests.
There are two different ways to run the tests:
- Open test/index.html in your browser or
- run
npm test
in the Terminal in the root path of the repository package.
The first one tests the browser integration, the second one the
node.js integration.
License
The JavaScript MD5 script is released under the
MIT license.