DEV Community

Cover image for Generating MD5 hashes on Node.js
Douglas Moura
Douglas Moura

Posted on • Originally published at douglasmoura.dev

7

Generating MD5 hashes on Node.js

You can create hashes in Node.js without the need to install any external library. Usually, I create the following utility function in the projects I work on:

/**
 * Hashes a string using md5
 *
 * @param {string} str
 * @returns {string}
 */
export const md5 = (str) => createHash('md5').update(str).digest('hex')
Enter fullscreen mode Exit fullscreen mode

And I use it to replace the md5 library whenever I come across it.

Note that you can create hashes for any algorithm supported by the OpenSSL version on your platform. On Linux and Mac, you can see which algorithms are available with the command openssl list -digest-algorithms.

Top comments (2)

Collapse
 
mrrishimeena profile image
Rishi Kumar

Why use this instead of the md5 library (stable and well-tested)?

Collapse
 
sscat profile image
Ivens Joris • Edited

because this is the default library, would you install a external library for the console.log?
So why do it if the language default library have this (stable, well tested, not dependent of external, but of the language)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay