DebugPointer
Published on

Create MD5 Hash in Javascript

Create MD5 Hash in Javascript

MD5 is (atleast when it was created) a standardized 1-way function that takes in data input of any form and maps it to a fixed-size output string, irrespective of the size of the input string.

Though it is used as a cryptographic hash function, it has been found to suffer from a lot of vulnerabilities.

The hash function generates the same output hash for the same input string. This means that, you can use this string to validate files or text or anything when you pass it across the network or even otherwise. MD5 can act as a stamp or for checking if the data is valid or not.

For example -

Input StringOutput Hash
hi49f68a5c8493ec2c0bf489821c21fc3b
debugpointerd16220bc73b8c7176a3971c7f73ac8aa
computer science is amazing! I love it.f3c5a497380310d828cdfc1737e8e2a3

If you are looking to generate md5 checksum in nodejs, please follow this article - Creating MD5 Hash in Node.js. If you looking to create an MD5 hash of a file, please follow the article where we discuss as to how to read a file buffer and create the hash.

The methods below are for creating it at client-side or browser.

Method 1 - Using crypto-js MD5 hash in HTML code

Here we will be using the above npm package directly in HTML code. We are using version 4.1.1 of the crypto-js package. Let's use the Cloudflare CDN links and use <script> tags to import core.min.js and md5.js scripts.

After that, you can use it in your code as CryptoJS.MD5(yourString). Here is an example demonstrating crypto-js and md5 implementation-

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MD5</title>
  </head>
  <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/core.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/md5.js"></script>
    <script>
      var hash = CryptoJS.MD5("Message")
      alert(hash)
    </script>
  </body>
</html>

Method 2 : Using crypto-js MD5 hash using ES6

Let's get into the modern approach first, using ES6 and crypto-js module in the frontend. Here, can import the function MD5 from the package. You can then use it directly to create an MD5 hash as shown in the example below-

First install the npm package-

$ bower install crypto-js

Let's configure it in require-

require.config({
    paths: {
        'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'
    }
});

Then you can use it in your code-

require(["crypto-js"], function (CryptoJS) {
    console.log(CryptoJS.MD5("Hello All", "Key"));
});

The output of the above code in the console will be-

94733212265f28ec2d0213188aeeaaf3876a996cb94f13f49968bc414ca47f1f

Method 3 - Using MD5 Hash implementation - Custom Implementation

For creating MD5 hash in the browser, we shall use the fastest implementation of md5 hash function created by Joseph Meyer.

Here, you will be adding the source of the md5.js file using the HTML <script> tag. Once it's done, invoking the MD5 hash function is as simple as md5(yourString).

Below is a simple example of the implementation-

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MD5</title>
    <script src="http://www.myersdaily.org/joseph/javascript/md5.js"></script>
  </head>
  <body>
    <script>
      console.log(md5("Hi!"))
      alert(md5("Hi!"))
    </script>
  </body>
</html>

It's actually very simple, just couple of things to do-

Import the library with the script tag (or download md5.js and host it on your own CDN)

<script src="http://www.myersdaily.org/joseph/javascript/md5.js"></script>

And then use it by assigning it to a variable

var md5hash = md5("Welcome to DebugPointer.com")

You can then use the md5hash variable in your program logic.

It's your choice, use what works best for you.

NOTE: Please do not use this to hash passwords and store it in your databases, prefer SHA-256 or SHA-512 or other superior cryptographic hash functions for the same.

Related articles

Let me summarize creating a hash in JavaScript and Nodejs, so that it becomes easy for you to refer-

I'm glad that you found the content useful. Happy Coding.