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");
@IPcorps
Copy link

IPcorps commented Mar 20, 2018

Still thanks!

@DrobenyukA
Copy link

DrobenyukA commented Mar 23, 2018

Thank you!

@webber0928
Copy link

webber0928 commented Apr 9, 2018

Thank you !!

@sakuraline
Copy link

sakuraline commented Jun 29, 2018

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

@aseevia
Copy link

aseevia commented Jul 6, 2018

Really helped, thanks!

@efkan
Copy link

efkan commented Aug 28, 2018

Thanks

@Stradivario
Copy link

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
Copy link

indatawetrust commented Sep 10, 2018

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

@pranatayudha
Copy link

pranatayudha commented Nov 22, 2018

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

can decrypt that data after using this method?

Copy link

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
Copy link

CQBinh commented Jan 10, 2019

Thank bro!

@karacas
Copy link

karacas commented Jan 13, 2019

nice!

@albertininm
Copy link

albertininm 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
Copy link

alfonmga commented Mar 21, 2019

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

@KDWangBoss
Copy link

KDWangBoss commented Apr 19, 2019

thanks

@denys-ua
Copy link

denys-ua commented May 11, 2019

Thanks, dude!

@abhijiy
Copy link

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
Copy link

diachedelic commented Jun 3, 2019

@ankur20us
Copy link

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
Copy link

smitthakkar1 commented Aug 8, 2019

Thnanks bro

@SyedFaraz55
Copy link

SyedFaraz55 commented Oct 11, 2019

Thank you so much @gistfile1.js

@ebnersilva
Copy link

ebnersilva commented Apr 14, 2020

Thank you so much!

@joseananio
Copy link

joseananio commented May 15, 2020

thanks !!

@JOELJOSEPHCHALAKUDY
Copy link

JOELJOSEPHCHALAKUDY commented Jun 4, 2020

Thank you

@ChandrakantDewangan
Copy link

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))

@niveinii
Copy link

niveinii commented Jul 15, 2021

Did someone manage to use the native crypto to encrypt an array? I want to do this:

let md5sum = crypto.createHash('md5');

let dataArray = ["foo", "bar"];

console.log(md5sum.update(dataArray).digest("hex"));

@ankur20us
Copy link

ankur20us commented Jul 15, 2021

Stringify array and then apply the hash

@niveinii
Copy link

niveinii commented Jul 15, 2021

@anakur20us

Thank you for the quick response, it worked like a charm!

@diegofcornejo
Copy link

diegofcornejo commented Sep 15, 2021

Thanks :)

@lucasmiranda-dev
Copy link

lucasmiranda-dev commented Feb 21, 2022

Thx man, you save my life

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment