Convert
nodejs
October 19, 20221 min read

Convert MD5 string to integer bits 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. But, what if you had to create a UUID out of it? Practical real-world use-cases can be - if you have a unique ID that you have in your database which you can map.

This implementation is for Node.js and server-side frameworks like Expressjs. In case you are looking for a JavaScript on the frontend (Javascript, React.js, Vue.js etc.,), implementation using CryptoJS module for JavaScript.

import { createHash } from "crypto";

function MD5toUUID(md5Hash) {
  return (
    md5Hash.substring(0, 8) +
    "-" +
    md5Hash.substring(8, 12) +
    "-" +
    md5Hash.substring(12, 16) +
    "-" +
    md5Hash.substring(16, 20) +
    "-" +
    md5Hash.substring(20)
  ).toLowerCase();
}

const yourString = "password";
const hash = createHash("md5").update(yourString).digest("hex");

console.log(MD5toUUID(hash));

In the above code, you can see that we are passing the hash string to the MD5toUUID function, which returns the UUID value of the md5 hash.

The output of the above code will be a UUID-

5f4dcc3b-5aa7-65d6-1d83-27deb882cf99

I'm glad that you found this article to create UUID from a MD5 hash string useful. Happy Coding.

Share this blog
Tagged in :
nodejs
crypto
md5
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
Satvik
Entrepreneur
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
September 14, 20223 min read
Next Article