Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeJS create md5 hash from string
var data = "do shash'owania";
var crypto = require('crypto');
crypto.createHash('md5').update(data).digest("hex");
@PhyrionX

This comment has been minimized.

Copy link

@PhyrionX PhyrionX commented Nov 12, 2015

Thank you bro!!!

@roccomuso

This comment has been minimized.

Copy link

@roccomuso roccomuso commented Feb 6, 2016

Cool ;)

@mpaluru

This comment has been minimized.

Copy link

@mpaluru mpaluru commented Feb 14, 2016

Like!
Short, simple and useful.

@nick13jaremek

This comment has been minimized.

Copy link

@nick13jaremek nick13jaremek commented Feb 19, 2016

Thank you very much, pretty useful 😄

@vajahath

This comment has been minimized.

Copy link

@vajahath vajahath commented Mar 2, 2016

yeah... useful... 👍
What are the possible parameters of digest() function besides hex ?

@indujuly

This comment has been minimized.

Copy link

@indujuly indujuly commented Mar 14, 2016

Thanks. very useful to my project.
npm install crypto-js

@tomfuertes

This comment has been minimized.

@aborn

This comment has been minimized.

Copy link

@aborn aborn commented May 26, 2016

Cool, Very Useful!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jun 19, 2016

Thank you

@mattkosoy

This comment has been minimized.

Copy link

@mattkosoy mattkosoy commented Jun 28, 2016

👍 gracias

@kevireilly

This comment has been minimized.

Copy link

@kevireilly kevireilly commented Aug 15, 2016

The one-liner of copy pasta:
require('crypto').createHash('md5').update('text to hash').digest('hex');

@gs-sgunasekaran-zz

This comment has been minimized.

Copy link

@gs-sgunasekaran-zz gs-sgunasekaran-zz commented Aug 19, 2016

Cool.. its very useful

@owntheweb

This comment has been minimized.

Copy link

@owntheweb owntheweb commented Aug 28, 2016

This saved me some time! Thanks. :D

@molinto

This comment has been minimized.

Copy link

@molinto molinto commented Nov 17, 2016

Thank you :)

@pittersnider

This comment has been minimized.

Copy link

@pittersnider pittersnider commented Dec 30, 2016

Cool, thank you so much!

@Server4001

This comment has been minimized.

Copy link

@Server4001 Server4001 commented Feb 6, 2017

Very helpful, thanks!

@VSG24

This comment has been minimized.

Copy link

@VSG24 VSG24 commented Apr 12, 2017

Cool.

@pragyandas

This comment has been minimized.

Copy link

@pragyandas pragyandas commented May 21, 2017

👍

@codingwesley

This comment has been minimized.

Copy link

@codingwesley codingwesley commented Jun 29, 2017

👍

@kryptstorm

This comment has been minimized.

Copy link

@kryptstorm kryptstorm commented Jul 4, 2017

Cool! Thanks bro!

@Crusader4Christ

This comment has been minimized.

Copy link

@Crusader4Christ Crusader4Christ commented Aug 15, 2017

For file it is better to use md5sum (for linux servers):

hash = exec("md5sum " + files.fd, {
   cwd: process.cwd(),
   encoding: 'utf8'
});
hash = hash.split(" ")[0];
@Booligoosh

This comment has been minimized.

Copy link

@Booligoosh Booligoosh commented Sep 12, 2017

Thanks so much!!! Legend ;)

@zisad

This comment has been minimized.

Copy link

@zisad zisad commented Oct 27, 2017

MD5: 24af3b1d894893d66ea3372edaaa1cdc

@beany-vu

This comment has been minimized.

Copy link

@beany-vu beany-vu commented Nov 17, 2017

How can I generate a similar result from frontend side? I use cryptoJs
CryptoJS.MD5(hostname + version + TOKEN_KEY).toString()
The results are different from each other.
Thanks in advance,

@Nastya-Piu

This comment has been minimized.

Copy link

@Nastya-Piu Nastya-Piu commented Jan 4, 2018

Thanks! 👍

@dixit7242

This comment has been minimized.

Copy link

@dixit7242 dixit7242 commented Jan 12, 2018

Hi,
crypto.createHash('md5').update(data).digest("hex");
the above will return 32 digit hex(32byte hash), what if i want just 16 digit hex. could anybody help me here?

@realprogrammer5000

This comment has been minimized.

Copy link

@realprogrammer5000 realprogrammer5000 commented Feb 1, 2018

Thanks for not making me install another dependency! I wish people made more gists like this!

@chieunhatnang

This comment has been minimized.

Copy link

@chieunhatnang chieunhatnang commented Feb 3, 2018

It saves me from the error: 'Digest method not supported'. Thanks!

@arifliaqat

This comment has been minimized.

Copy link

@arifliaqat arifliaqat commented Feb 14, 2018

Thanks its saving lot of my time

@IPcorps

This comment has been minimized.

Copy link

@IPcorps IPcorps commented Mar 20, 2018

Still thanks!

@DrobenyukA

This comment has been minimized.

Copy link

@DrobenyukA DrobenyukA commented Mar 23, 2018

Thank you!

@webber0928

This comment has been minimized.

Copy link

@webber0928 webber0928 commented Apr 9, 2018

Thank you !!

@sakuraline

This comment has been minimized.

Copy link

@sakuraline sakuraline commented Jun 29, 2018

buenas noches quisiera saber como encriptar con md5 en un chat web con node js

@aseevia

This comment has been minimized.

Copy link

@aseevia aseevia commented Jul 6, 2018

Really helped, thanks!

@efkan

This comment has been minimized.

Copy link

@efkan efkan commented Aug 28, 2018

Thanks

@Stradivario

This comment has been minimized.

Copy link

@Stradivario Stradivario commented Sep 7, 2018

Thank you! Usable inside https://github.com/rxdi/main!

Searching for good implementation inside browser for the same purpose...

@indatawetrust

This comment has been minimized.

Copy link

@indatawetrust indatawetrust commented Sep 10, 2018

const md5 = data => crypto.createHash('md5').update(data).digest("hex")
@pranatayudha

This comment has been minimized.

Copy link

@pranatayudha pranatayudha commented Nov 22, 2018

const md5 = data => crypto.createHash('md5').update(data).digest("hex")

can decrypt that data after using this method?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 27, 2018

const md5 = data => crypto.createHash('md5').update(data).digest("hex")

can decrypt that data after using this method?

@pranatayudha No you can't. md5 is a hash function

@CQBinh

This comment has been minimized.

Copy link

@CQBinh CQBinh commented Jan 10, 2019

Thank bro!

@karacas

This comment has been minimized.

Copy link

@karacas karacas commented Jan 13, 2019

nice!

@albertmourato

This comment has been minimized.

Copy link

@albertmourato albertmourato commented Feb 27, 2019

Hey @beany-vu, were you able to use crypto from the frontend side properly? I'm trying to do the same but it's not working well...

@alfonmga

This comment has been minimized.

Copy link

@alfonmga alfonmga commented Mar 21, 2019

@albertmourato crypto is a NodeJS module, so you can't use it in the frontend side.

@KDWangBoss

This comment has been minimized.

Copy link

@KDWangBoss KDWangBoss commented Apr 19, 2019

thanks

@denys-ua

This comment has been minimized.

Copy link

@denys-ua denys-ua commented May 11, 2019

Thanks, dude!

@abhijiy

This comment has been minimized.

Copy link

@abhijiy abhijiy commented May 29, 2019

Is it possible to decrypt the string? Can someone give me sample code for that? I have used for encryption
crypto.createHash('md5').update(req.body.userType.trim() + constants.APIKEY).digest("hex")

@diachedelic

This comment has been minimized.

@ankur20us

This comment has been minimized.

Copy link

@ankur20us ankur20us commented Jun 10, 2019

Is it possible to decrypt the string? Can someone give me sample code for that? I have used for encryption
crypto.createHash('md5').update(req.body.userType.trim() + constants.APIKEY).digest("hex")
Hashing is not encryption. You can't come to original state once you get the hashed version.

@smitthakkar1

This comment has been minimized.

Copy link

@smitthakkar1 smitthakkar1 commented Aug 8, 2019

Thnanks bro

@SyedFaraz55

This comment has been minimized.

Copy link

@SyedFaraz55 SyedFaraz55 commented Oct 11, 2019

Thank you so much @gistfile1.js

@ebnersilva

This comment has been minimized.

Copy link

@ebnersilva ebnersilva commented Apr 14, 2020

Thank you so much!

@joseananio

This comment has been minimized.

Copy link

@joseananio joseananio commented May 15, 2020

thanks !!

@JOELJOSEPHCHALAKUDY

This comment has been minimized.

Copy link

@JOELJOSEPHCHALAKUDY JOELJOSEPHCHALAKUDY commented Jun 4, 2020

Thank you

@ChandrakantDewangan

This comment has been minimized.

Copy link

@ChandrakantDewangan ChandrakantDewangan commented Jul 31, 2020

Method 1

const crypto = require('crypto');
let hash = crypto.createHash('md5').update('My Message').digest("hex");
console.log(hash);//069840f6917e85a02167febb964f0041

Method 2

const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

function encrypt(text) {
 let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
 let encrypted = cipher.update(text);
 encrypted = Buffer.concat([encrypted, cipher.final()]);
 return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}

function decrypt(text) {
 let iv = Buffer.from(text.iv, 'hex');
 let encryptedText = Buffer.from(text.encryptedData, 'hex');
 let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
 let decrypted = decipher.update(encryptedText);
 decrypted = Buffer.concat([decrypted, decipher.final()]);
 return decrypted.toString();
}

var hw = encrypt("Some serious stuff")
console.log(hw)
console.log(decrypt(hw))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment