Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MD5 problem converting from Java

    Hi,
    I am trying to convert a Java api into javascript, but am struggling to get the md5 output to match on my javascript.

    The java code for this is:
    [CODE]
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update(CLIENT_API_KEY.getBytes("UTF-8"));

    byte[] array = md.digest();
    [ICODE]
    Where CLIENT_API_KEY = "81fce185dc"

    I then have an additional bit of code to output the hex so that I can compare with javascript:
    StringBuffer hexString = new StringBuffer();
    for (int i=0;i<array.length;i++) {
    String hex = Integer.toHexString(0xFF & array[i]);
    if(hex.length()==1)
    hexString.append('0');

    hexString.append(hex);
    }
    In java, this is returning an array of [42,68,34,-104 ... ]
    and the hex works out to 2a442298dfdc6a5f4dbb76697c2a232f

    In javascript I am doing:
    Code:
    var hashString = CryptoJS.MD5(utf8.join("")).toString();
    I have also tried a couple of other md5 converters which give the same output, which is a hex of: af5b87191828401b7d486497694b3e9b

    I have also tried this by just passing in the string without using the utf8 array just in case the CryptoJS script is doing this automatically which results in: df4752c78f693d5292e3ce535f04624e

    Does anyone know whats going wrong here? what can I do to ensure my javascript matches the output from the java.

    Any help appreciated. Thanks,

  • #2
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rudi.rad View Post
    Hi,
    I am trying to convert a Java api into javascript, but am struggling to get the md5 output to match on my javascript.
    Check this out: http://phpjs.org/functions/md5/

    It's MD5 in Javascript. Hope this helps.

    Code:
    /*****
     * md5.js
     * Downloaded from: http://phpjs.org/functions/md5
     * http://kevin.vanzonneveld.net
     * original by: Webtoolkit.info (http://www.webtoolkit.info/)
     * namespaced by: Michael White (http://getsprink.com)
     * tweaked by: Jack
     * improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
     * input by: Brett Zamir (http://brett-zamir.me)
     * bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
     * depends on: utf8_encode
     * example 1: md5('Kevin van Zonneveld');
     * returns 1: '6e658d4bfcb59cc13f96c14450ac40b9'
     ****/
    function md5(str) {
        var xl;
        var rotateLeft = function (lValue, iShiftBits) {
            return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
        };
        var addUnsigned = function (lX, lY) {
            var lX4, lY4, lX8, lY8, lResult;
            lX8 = (lX & 0x80000000);
            lY8 = (lY & 0x80000000);
            lX4 = (lX & 0x40000000);
            lY4 = (lY & 0x40000000);
            lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
            if (lX4 & lY4) {
                return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
            }
            if (lX4 | lY4) {
                if (lResult & 0x40000000) {
                    return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
                } else {
                    return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
                }
            } else {
                return (lResult ^ lX8 ^ lY8);
            }
        };
        var _F = function (x, y, z) {
            return (x & y) | ((~x) & z);
        };
        var _G = function (x, y, z) {
            return (x & z) | (y & (~z));
        };
        var _H = function (x, y, z) {
            return (x ^ y ^ z);
        };
        var _I = function (x, y, z) {
            return (y ^ (x | (~z)));
        };
        var _FF = function (a, b, c, d, x, s, ac) {
            a = addUnsigned(a, addUnsigned(addUnsigned(_F(b, c, d), x), ac));
            return addUnsigned(rotateLeft(a, s), b);
        };
        var _GG = function (a, b, c, d, x, s, ac) {
            a = addUnsigned(a, addUnsigned(addUnsigned(_G(b, c, d), x), ac));
            return addUnsigned(rotateLeft(a, s), b);
        };
        var _HH = function (a, b, c, d, x, s, ac) {
            a = addUnsigned(a, addUnsigned(addUnsigned(_H(b, c, d), x), ac));
            return addUnsigned(rotateLeft(a, s), b);
        };
        var _II = function (a, b, c, d, x, s, ac) {
            a = addUnsigned(a, addUnsigned(addUnsigned(_I(b, c, d), x), ac));
            return addUnsigned(rotateLeft(a, s), b);
        };
        var convertToWordArray = function (str) {
            var lWordCount;
            var lMessageLength = str.length;
            var lNumberOfWords_temp1 = lMessageLength + 8;
            var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
            var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
            var lWordArray = new Array(lNumberOfWords - 1);
            var lBytePosition = 0;
            var lByteCount = 0;
            while (lByteCount < lMessageLength) {
                lWordCount = (lByteCount - (lByteCount % 4)) / 4;
                lBytePosition = (lByteCount % 4) * 8;
                lWordArray[lWordCount] = (lWordArray[lWordCount] | (str.charCodeAt(lByteCount) << lBytePosition));
                lByteCount++;
            }
            lWordCount = (lByteCount - (lByteCount % 4)) / 4;
            lBytePosition = (lByteCount % 4) * 8;
            lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
            lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
            lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
            return lWordArray;
        };
        var wordToHex = function (lValue) {
            var wordToHexValue = "",
            wordToHexValue_temp = "",
            lByte, lCount;
            for (lCount = 0; lCount <= 3; lCount++) {
                lByte = (lValue >>> (lCount * 8)) & 255;
                wordToHexValue_temp = "0" + lByte.toString(16);
                wordToHexValue = wordToHexValue + wordToHexValue_temp.substr(wordToHexValue_temp.length - 2, 2);
            }
            return wordToHexValue;
        };
        var x = [],
        k, AA, BB, CC, DD, a, b, c, d, S11 = 7,
        S12 = 12,
        S13 = 17,
        S14 = 22,
        S21 = 5,
        S22 = 9,
        S23 = 14,
        S24 = 20,
        S31 = 4,
        S32 = 11,
        S33 = 16,
        S34 = 23,
        S41 = 6,
        S42 = 10,
        S43 = 15,
        S44 = 21;
        str = this.utf8_encode(str);
        x = convertToWordArray(str);
        a = 0x67452301;
        b = 0xEFCDAB89;
        c = 0x98BADCFE;
        d = 0x10325476;
        xl = x.length;
        for (k = 0; k < xl; k += 16) {
            AA = a;
            BB = b;
            CC = c;
            DD = d;
            a = _FF(a, b, c, d, x[k + 00], S11, 0xD76AA478);
            d = _FF(d, a, b, c, x[k + 01], S12, 0xE8C7B756);
            c = _FF(c, d, a, b, x[k + 02], S13, 0x242070DB);
            b = _FF(b, c, d, a, x[k + 03], S14, 0xC1BDCEEE);
            a = _FF(a, b, c, d, x[k + 04], S11, 0xF57C0FAF);
            d = _FF(d, a, b, c, x[k + 05], S12, 0x4787C62A);
            c = _FF(c, d, a, b, x[k + 06], S13, 0xA8304613);
            b = _FF(b, c, d, a, x[k + 07], S14, 0xFD469501);
            a = _FF(a, b, c, d, x[k + 08], S11, 0x698098D8);
            d = _FF(d, a, b, c, x[k + 09], S12, 0x8B44F7AF);
            c = _FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
            b = _FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
            a = _FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
            d = _FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
            c = _FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
            b = _FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
            a = _GG(a, b, c, d, x[k + 01], S21, 0xF61E2562);
            d = _GG(d, a, b, c, x[k + 06], S22, 0xC040B340);
            c = _GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
            b = _GG(b, c, d, a, x[k + 00], S24, 0xE9B6C7AA);
            a = _GG(a, b, c, d, x[k + 05], S21, 0xD62F105D);
            d = _GG(d, a, b, c, x[k + 10], S22, 0x02441453);
            c = _GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
            b = _GG(b, c, d, a, x[k + 04], S24, 0xE7D3FBC8);
            a = _GG(a, b, c, d, x[k + 09], S21, 0x21E1CDE6);
            d = _GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
            c = _GG(c, d, a, b, x[k + 03], S23, 0xF4D50D87);
            b = _GG(b, c, d, a, x[k + 08], S24, 0x455A14ED);
            a = _GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
            d = _GG(d, a, b, c, x[k + 02], S22, 0xFCEFA3F8);
            c = _GG(c, d, a, b, x[k + 07], S23, 0x676F02D9);
            b = _GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
            a = _HH(a, b, c, d, x[k + 05], S31, 0xFFFA3942);
            d = _HH(d, a, b, c, x[k + 08], S32, 0x8771F681);
            c = _HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
            b = _HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
            a = _HH(a, b, c, d, x[k + 01], S31, 0xA4BEEA44);
            d = _HH(d, a, b, c, x[k + 04], S32, 0x4BDECFA9);
            c = _HH(c, d, a, b, x[k + 07], S33, 0xF6BB4B60);
            b = _HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
            a = _HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
            d = _HH(d, a, b, c, x[k + 00], S32, 0xEAA127FA);
            c = _HH(c, d, a, b, x[k + 03], S33, 0xD4EF3085);
            b = _HH(b, c, d, a, x[k + 06], S34, 0x04881D05);
            a = _HH(a, b, c, d, x[k + 09], S31, 0xD9D4D039);
            d = _HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
            c = _HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
            b = _HH(b, c, d, a, x[k + 02], S34, 0xC4AC5665);
            a = _II(a, b, c, d, x[k + 00], S41, 0xF4292244);
            d = _II(d, a, b, c, x[k + 07], S42, 0x432AFF97);
            c = _II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
            b = _II(b, c, d, a, x[k + 05], S44, 0xFC93A039);
            a = _II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
            d = _II(d, a, b, c, x[k + 03], S42, 0x8F0CCC92);
            c = _II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
            b = _II(b, c, d, a, x[k + 01], S44, 0x85845DD1);
            a = _II(a, b, c, d, x[k + 08], S41, 0x6FA87E4F);
            d = _II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
            c = _II(c, d, a, b, x[k + 06], S43, 0xA3014314);
            b = _II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
            a = _II(a, b, c, d, x[k + 04], S41, 0xF7537E82);
            d = _II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
            c = _II(c, d, a, b, x[k + 02], S43, 0x2AD7D2BB);
            b = _II(b, c, d, a, x[k + 09], S44, 0xEB86D391);
            a = addUnsigned(a, AA);
            b = addUnsigned(b, BB);
            c = addUnsigned(c, CC);
            d = addUnsigned(d, DD);
        }
        var temp = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);
        return temp.toLowerCase();
    };
    
    function utf8_encode(argString) {
        if (argString === null || typeof argString === 'undefined') {
            return '';
        }
        var string = (argString + ''); /* .replace(/\r\n/g, '\n').replace(/\r/g, '\n'); */
        var utftext = '',
        start, end, stringl = 0;
        start = end = 0;
        stringl = string.length;
        for (var n = 0; n < stringl; n++) {
            var c1 = string.charCodeAt(n);
            var enc = null;
            if (c1 < 128) {
                end++;
            } else if (c1 > 127 && c1 < 2048) {
                enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
            } else {
                enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
            }
            if (enc !== null) {
                if (end > start) {
                    utftext += string.slice(start, end);
                }
                utftext += enc;
                start = end = n + 1;
            }
        }
        if (end > start) {
            utftext += string.slice(start, stringl);
        }
        return utftext;
    };
    (edit to add): The code above is the same as the website I linked for you, but with the dependency "utf8_encode" function added.

    (edit to add): Just tested it here. md5('81fce185dc'); returns "df4752c78f693d5292e3ce535f04624e" which is I think what you're after?
    Last edited by Krupski; 07-03-2013 at 05:17 AM. Reason: added code & comment
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Krupski View Post
    Check this out: Just tested it here. md5('81fce185dc'); returns "df4752c78f693d5292e3ce535f04624e" which is I think what you're after?
    Thanks, but df4752c78f693d5292e3ce535f04624e is what I am already getting for my current javascript MD5. (I had a look and my existing script does do the UFT_8 conversion) This still doesn't match with 2a442298dfdc6a5f4dbb76697c2a232f which is the MD5 returned by the java code.

    So just to clarify .. because I do have a tendency to waffle.

    I need to match:
    2a442298dfdc6a5f4dbb76697c2a232f
    Array [42,68,34,-104 ... ]

    I am returning df4752c78f693d5292e3ce535f04624e which is the same as I get with almost any of the online MD5 scripts. This seems to imply that something else is different between the java and javascript code.. I just cannot think what.

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rudi.rad View Post
    Thanks, but df4752c78f693d5292e3ce535f04624e is what I am already getting for my current javascript MD5. (I had a look and my existing script does do the UFT_8 conversion) This still doesn't match with 2a442298dfdc6a5f4dbb76697c2a232f which is the MD5 returned by the java code.

    So just to clarify .. because I do have a tendency to waffle.

    I need to match:
    2a442298dfdc6a5f4dbb76697c2a232f
    Array [42,68,34,-104 ... ]

    I am returning df4752c78f693d5292e3ce535f04624e which is the same as I get with almost any of the online MD5 scripts. This seems to imply that something else is different between the java and javascript code.. I just cannot think what.
    Are there possibly other characters in the "key" you are passing to the MD5 code (\r or \n) that you aren't seeing?
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    how are we to know if it works if we don't know the input? a java snip doesn't count, we need two strings...

    please post just the input and desired output so we can find something that works for your case.
    i think it might actually be working until you mod the "js" output in java using &255, so the actual java input is important here.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks rnd me,
    Changed the input to make it a bit more insular/readable and this seems to have resolved the issue. Obviously something else is happening to my string in the java code that I have missed. Will trawl back through the code and have another look.

    Now in javascript I have:

    Code:
    var hashString = CryptoJS.MD5("81fce185dc");
    alert(hashString);
    Which is returning: df4752c78f693d5292e3ce535f04624e

    in Java the code is:

    Code:
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update("81fce185dc".getBytes("UTF-8"));
    byte[] array = md.digest();
    Which is now returning: [-33, 71, 82, -57, -113, 105, 61, 82, -110, -29, -50, 83, 95, 4, 98, 78] - which should convert to:
    df4752c78f693d5292e3ce535f04624e

    YAY!

    Thanks again

  • #7
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rnd me View Post
    how are we to know if it works if we don't know the input? a java snip doesn't count, we need two strings...

    please post just the input and desired output so we can find something that works for your case.
    i think it might actually be working until you mod the "js" output in java using &255, so the actual java input is important here.
    It was in the first post:

    Quote Originally Posted by rudi.rad View Post
    Code:
    Where CLIENT_API_KEY = "81fce185dc"
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #8
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    AAgh, Have stumbled upon another problem. I am hoping someone may have a solution for..
    I thought my MD5 was working perfectly until I pulled all of the elements together. The actual use of the MD5 in the java code was to create a hash of an image combined with the client Key. But the image is put into a byte array. so the bitmap character becomes (byte) [-1] (signed: 255)

    Java creates a hash of this byte as: 00594fd4f42ba43fc1ca0427a0576295

    But Javascript and most other online generators (php etc) give: f3f7437e8c1f303fc6742d9368b36cf6

    Why is this? and is there anything I can do to give: 00594fd4f42ba43fc1ca0427a0576295 in javascript?

  • #9
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again guys. Got the other issue sorted now. Found an md5 script that processes bytes in a similar way to java rather than strings. Cheers.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •