Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple MD5 hash of a String

How can I generate an MD5 hash of a String, from JavaScript running on PhantomJS ?

I tried npm install -g crypto but the crypto module is not found.

like image 407
Thomas Decaux Avatar asked Jun 25 '13 13:06

Thomas Decaux


1 Answers

PhantomJs does not natively support MD5 but you can easily inject external script code using phantom.injectJs.

You have just a find an implementation in javascript. MD5 is quite popular and not too difficult to implent. So there are many implementations such as crypto-js or wbond/md5-js.

A very basic script using CryptoJS could be

var system = require('system');

if (system.args.length != 2) {
    console.log("Usage: phantomjs md5_test.js input");
} else {
    if(phantom.injectJs('md5.js')) {
        console.log(CryptoJS.MD5(system.args[1]));
        phantom.exit();
    }
}
like image 78
Cybermaxs Avatar answered Sep 23 '22 06:09

Cybermaxs