0

I have Javascript md5 on site auth.

I need to implement only this function:

function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}

I need help with following methods:

Convert an array of little-endian words to a hex string:

function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

Convert a string to an array of little-endian words If chrsz is ASCII, characters >255 have their hi-byte silently ignored.

function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}  
tshepang
  • 12,111
  • 21
  • 91
  • 136
Romek
  • 16
  • 2
  • I removed the `Java` tag as it seems to be irreilevant... – home Aug 10 '12 at 18:27
  • Why? Need java implementation for javascript methods, people must know java for properly answer! – Romek Aug 10 '12 at 18:33
  • So wheres the Java code that you've tried? I know this is a bit of a tricky one, but actually a lot of the stuff transers over! Retagged java because home is rediculous. Clearly Romek is looking for a Java implementation of a javascript function, presumably for a web app or the like. – VoronoiPotato Aug 10 '12 at 18:44
  • because i don't know javacript and i can't implement same in Java – Romek Aug 10 '12 at 19:00

2 Answers2

1

If you're just looking to implement MD5, that's something which is built in to java.

https://stackoverflow.com/a/415971/576519

Community
  • 1
  • 1
VoronoiPotato
  • 3,113
  • 20
  • 30
  • I have my own md5 implementation class(with key, without key and more others variants), but still need same algorithm auth implementation for android app. I don't know javacript and i cant implement same in Java. Regards =P – Romek Aug 10 '12 at 18:52
0

I have developed a simple Java MD5 function that is compatible with a standard JavaScript function you can download the class from: http://developersfound.com/HexMD5.zip

Here is the code:

/* This MD5 hash class is compatible with the JavaScript MD5 has code found at http://pajhome.org.uk/crypt/md5/md5.html */
package com.DevFound;

import java.security.MessageDigest;

public class HexMD5 {
    public static String getMD5Str(String inputVal)throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(inputVal.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<byteData.length;i++) {
            String hex=Integer.toHexString(0xff & byteData[i]);
            if(hex.length()==1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }
}

I haven't tested this code with your particular JavaScript md5 function but I have listed the function that it is compatible with in the comment at the top of the code.

user2288580
  • 2,210
  • 23
  • 16