perlAffen has asked for the wisdom of the Perl Monks concerning the following question:

I am using curl to script a login to a webserver. One of the things the login process requires is running a javascript to encode the password. It is called MD5.js , some RSA message digest algorithm. It is called like this
function validate_form(form) { var passwd_enc = calcMD5(form.password.value); var final_to_encode = passwd_enc +<br> form.one_time_token.value; form.encoded_pw.value = calcMD5(final_to_encode); form.password.value = ""; return true; }
so this MD5 encodes the password, then MD5 encodes it again after adding a one_time_token. I assume it is concatentation ?
So, how can I use perl to run this js program to encode what I need ?
Thanks for your patience getting to the perl bit.

Edited by planetscape - added code tags

Replies are listed 'Best First'.
Re: perl to run a js script
by jonadab (Parson) on Apr 13, 2006 at 02:44 UTC

    If you really needed to run the Javascript, you'd need a Javascript engine. However, if all you need is to do what the Javascript does, for just this one page, you could translate the Javascript into Perl. For common hashing algorithms like MD5, there are of course modules on the CPAN that will make the translation rather easier. Typically, the Perl code will end up being much shorter than the Javascript it replaces. This is the approach I would usually recommend, if you are only scripting specific sites and just need to do enough of what their client-side scripts do to get the forms submitted.

    If you were trying to create an actual web browser in Perl, or needed to write a general web crawler that would work on most any site, then you'd want to look at using an actual Javascript engine, but that would probably be a larger task.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
Re: perl to run a js script
by GrandFather (Saint) on Apr 13, 2006 at 02:49 UTC

    If you can access the fields from the form then you can do the lot in Perl:

    use Digest::MD5 qw(md5); sub validate_form { my $password= shift; my $token = shift; return MD5 (MD5 ($password) . $token); }

    DWIM is Perl's answer to Gödel
      this is a misleading title... i thought someone found a way to run perl as a java script as part of a web page. That would be impressive