/*
 * A WML Script implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Copyright (C) Simon Wistow, 2001
 */

use url md5aux "md5_aux.wmlsc";




function calcMD5(str)
{
  var x = md5aux#str2blks_MD5(str);
  var a = 0x67452301;
  var b = 0xEFCDAB89;
  var c = 0x98BADCFE;
  var d = 0x10325476;

  for(var i = 0; i < String.elements(x, '|'); i += 16)
  {

    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    var abcd = a + '|' + b + '|' + c + '|' + d;


    abcd = md5aux#ff_four(abcd, x, i, 0,  0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE);
    abcd = md5aux#ff_four(abcd, x, i, 4,  0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501);
    abcd = md5aux#ff_four(abcd, x, i, 8,  0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE);
    abcd = md5aux#ff_four(abcd, x, i, 12, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821);


    abcd = md5aux#gg_four(abcd, x, i, 1,  0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA);
    abcd = md5aux#gg_four(abcd, x, i, 5,  0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8);
    abcd = md5aux#gg_four(abcd, x, i, 9,  0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED);
    abcd = md5aux#gg_four(abcd, x, i, 13, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A);



    abcd = md5aux#hh_four(abcd, x, i, 5,  0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C);
    abcd = md5aux#hh_four(abcd, x, i, 1,  0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70);
    abcd = md5aux#hh_four(abcd, x, i, 13, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05);
    abcd = md5aux#hh_four(abcd, x, i, 9,  0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665);

    abcd = md5aux#ii_four(abcd, x, i, 0,  0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039);
    abcd = md5aux#ii_four(abcd, x, i, 12, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1);
    abcd = md5aux#ii_four(abcd, x, i, 8,  0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1);
    abcd = md5aux#ii_four(abcd, x, i, 4,  0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391);

    a =  md5aux#add( md5aux#elt(abcd, 0), olda);
    b =  md5aux#add( md5aux#elt(abcd, 1), oldb);
    c =  md5aux#add( md5aux#elt(abcd, 2), oldc);
    d =  md5aux#add( md5aux#elt(abcd, 3), oldd);


 }

  return md5aux#rhex(a) + md5aux#rhex(b) + md5aux#rhex(c) + md5aux#rhex(d);
}


extern function hash (var_to_set, string)
{



        WMLBrowser.setVar(var_to_set, calcMD5(URL.unescapeString(string)));
        WMLBrowser.refresh ();
}