October 18, 20222 min read

Create MD5 hash of a dict in JavaScript

As we've seen in earlier posts, you can create an MD5 hash of a string and generate a hash in string type.

In JavaScipt, an Object is a data structure that stores key-value pairs. The keys are used to access the values.

Example -

const person = { name: "Vamana", age: 20 };

In case you are working on the frontend (JavaScript, React, Vue or so), you can work with the CryptoJS library to create MD5 hash of an Object.

But, what if you had to create an md5 hash of the person object above? We can do it by first converting the Object type to string format. Note that, a object doesn't maintain the order of keys (key:values), so the value can change randomly when you try to use it as a complete object. For this reason, we will sort the keys and then convert it to a string, to make sure that the order of keys is consistent always. We do this by sorting the person object to orderedPerson object and then performing a JSON.stringify() to stringify the JSON object as shown in the example below.

Here is an example with complete code of the above mentioned implementation.

import { createHash } from "crypto";
const person = { name: "Vamana", age: 20 };
const orderedPerson = Object.fromEntries(Object.entries(person).sort());
const hash = createHash("md5")

The output of the above JavaScript code will return a MD5 hash of the stringified version of the object-


I'm glad that you found this article to create MD5 hash of an object in JavaScript useful. Happy Coding.

Share this blog
Tagged in :
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article