How to use MD5 hashes in JavaScript

In this article, you are going learn to use the MD5 hashes in JavaScript. The MD5 hash function was originally created as a secure cryptographic algorithm to authenticate digital signatures.

However, it has since been deprecated for such purposes due to its vulnerabilities.

Instead, it is commonly used for non-cryptographic checksums to check data integrity and identify unintended data corruption. This means that it is frequently employed to ensure the integrity of files.

Although JavaScript lacks built-in cryptographic utilities, the same functionality can be obtained by implementing third-party libraries.

What is JavaScript MD5?

This JavaScript MD5 is designed to work smoothly in different server-side environments such as Node.js, module loaders like RequireJS or webpack, and across all web browsers.

How to add MD5 to your Project?

When your project needs a package manager, you can simply install it using NPM through the following command:

npm install blueimp-md5

If you come from the old school, simply add the MD5 script to your document through including the original or shortened version from the official repository (available here).

Here’s an example code:

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

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

To get further details regarding this library, kindly visit its official GitHub repository by clicking here.

How to hash using the MD5

To start with, make sure you have already installed the MD5 library in your project. If it’s already installed, the next step is to import it.

For Example:

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

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

Once it is already importing it, its usage will be quite simple. md5 is a function that takes up to 3 parameters:

  • value (string)
    • This is the input value that will be hashed using the MD5 algorithm.
  • key (string)
    • If you need to use the HMAC to key-hash a string, provide it as the second argument.
  • raw (Boolean)
    • This Boolean defines whether the hash should be in raw or hex encoded format (false by default).

Using MD5 hash

You can simply make the MD5 hash of a given string through providing it as first argument:

Let’s see an example code:


let hash = md5("password");

// contains: "5f4dcc3b5aa765d61d8327deb882cf99"

Using HMAC-MD5

You can simply build the HMAC-MD5 hash of given string by providing as first argument its value and as second argument its key:

For example:

let hash = md5("passoword", "itsourcecode");

Utilizing Raw MD5 hash

You can easily generate the raw MD5 hash of a given string by providing it as the first argument, setting the second argument to null, and the third argument to true.

Here’s an example:

let rawhash = md5("password", null, true);

Utilizing Raw HMAC-MD5

You can simply generate the raw HMAC-MD5 hash for a given string by providing the string as the first argument, the key as the second argument, and setting the third parameter to true.

Example that uses the Raw HMAC-MD5 :

let rawhash = md5('password', 'itsourcecode', true)

Conclusion

In conclusion, you have learned how to use MD5 hashes in JavaScript, what is the meaning of JavaScript MD5.

Also, we have discussed how to add MD5 to your project and how to hash using the MD5.

Additional Resources

Leave a Comment