Learn how to create easily the MD5 hash of any string in JavaScript.

How to create MD5 hashes in JavaScript

The MD5 hash function was designed for use as a secure cryptographic hash algorithm for authenticating digital signatures, however, it's been deprecated for uses different than non-cryptographic checksum to verify data integrity and detect unintentional data corruption. This means, it's quite usual to use this algorithm to prove the integrity of a file. In JavaScript, there's no cryptographic set of utilities, however, it's possible to implement the same functionality using a third-party library, in this case the MD5 library written by @blueimp which is available as an open-source repository, and whose source code is available at Github here. This JavaScript implementation of MD5 is compatible with server-side environments like Node.js, module loaders like RequireJS or webpack, and all web browsers.

How to include MD5 in your project

If your project uses a package manager, you can easily install it using NPM with the following command:

npm install blueimp-md5

Alternatively, if you are from the old school, just include the MD5 script (you can obtain it the original and minified version from the official repo here) in your document like this:

<!-- Include MD5 library -->
<script src="md5.js"></script>

<!-- Include MD5 minified version -->
<script src="md5.min.js"></script>

For more information about this library, please don't forget to visit the official repository at Github here.

How to hash using the MD5 algorithm

The first thing you need to do is to import the MD5 library in your project if you installed it already:

// If you are using a package manager, require the package
const md5 = require("blueimp-md5");

// If you are using ES6
import { md5 } from "blueimp-md5";

// Alternatively in the browser md5 will be available globally in the window

After importing it, its usage will be pretty straightforward. md5 is a function that expects up to 3 parameters:

  • value (string): the value that will be hashed using the MD5 algorithm.
  • key (string): if you want to use HMAC to key-hash a string, provide it as second argument.
  • raw (boolean): a boolean that determines whether the hash should be raw or hex encoded (false by default).

MD5 hash (hex-encoded)

You can easily create the MD5 hash of any given string providing it as first argument:

// contains: "dc599a9972fde3045dab59dbd1ae170b"
let hash = md5("carlos");

HMAC-MD5 (hex-encoded)

You can easily create the HMAC-MD5 hash of any given string providing as first argument its value and as second argument its key:

// contains: "80244576c6c4e060a8e14b124cebaaa4"
// md5("key", "value")
let hash = md5("carlos", "ourcodeworld");

MD5 hash (raw)

You can easily create the raw MD5 hash of any given string providing it as first argument, as second argument a null value and the third parameter as true:

// contains: "ÜYš™rýã\u0004]«YÛÑ®\u0017\u000b"
let rawhash = md5("carlos", null, true);

HMAC-MD5 (raw)

You can easily create the raw HMAC-MD5 hash of any given string providing it as first argument, as second argument its key and the third parameter as true:

// contains: "€$EvÆÄà`¨áK\u0012L몤"
let rawhash = md5('carlos', 'ourcodeworld', true)

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors