Create
nodejs
September 14, 20223 min read

Create MD5 Hash of a File in Node.js

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 want to generate md5 checksum in JavaScript i.e., client side (browser), please follow this article - Create MD5 Hash in JavaScript

Node.js crypto module provides cryptographic functions to help you secure code and data in Node.js. It includes a set of wrappers for OpenSSL’s hash, HMAC, cipher, decipher, sign, and verify functions.

crypto is built into Node.js, so there is not configuration or custom implementation needed.

For creating MD5 hash in nodejs script/code, we shall use the default crypto module that comes packaged with nodejs.

Creating MD5 Hash of a File

Now let's learn how to create an MD5 hash of a file. File can be any file, a text file, a JSON file, data file etc.,

You can either require the crypto module-

const crypto = require("crypto");

and you can use it as cryoto.createHash() function.

or also use the modern import to import the crypto module-

import { createHash } from "crypto";

Here, let's consider a text file for example - hello.txt having content - Welcome to Debugpointer. Let's read the contents of the file using the fs module and read the file in a synchronous manner.

import { createHash } from "crypto";
const fs = require("fs");
const buff = fs.readFileSync("hello.txt");
const hash = createHash("md5").update(buff).digest("hex");
console.log(hash);

The output of the above script will be an MD5 hash when you run the command node index.js in your shell-

ab04f62105f04a0547f5c451a81f2c90

If you looking to create an MD5 hash of a string, please follow the article where we discuss about this.

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.

It's as simple as that! You do not need any fancy npm library for creating an MD5 hash in Node.js.

I'm glad that you found the content 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 22, 20222 min read
Next Article