Md5 from java to php

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

smilefr
Posts: 58
Joined: 23 Mar 2009, 16:50
Location: France

Md5 from java to php

Postby smilefr » 10 Jan 2011, 07:52

I am trying to generate the same MD5 code as the md5() function in php.

Therefore when i use this code, the result is different:

Code: Select all

public static String md5(String input) throws NoSuchAlgorithmException {
        String result = input;
        if(input != null) {
            MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        }
        return result;
    }


Any help would be greatly appreciated.
janux
Posts: 28
Joined: 21 Sep 2010, 19:11

Postby janux » 10 Jan 2011, 08:25

Try something like this:

Code: Select all

   private static String md5(String input) throws NoSuchAlgorithmException {
      String res = "";
      MessageDigest md = MessageDigest.getInstance("MD5");
      try {
         md.reset();
         md.update(input.getBytes());
         byte[] bres = md.digest();
         StringBuffer hexString = new StringBuffer();
         for (int i=0;i<bres.length;i++) {
            hexString.append(Integer.toHexString(0xFF & bres[i]));
         }
         res = hexString.toString();         
      } catch (Exception ex) {
         
      }   
      return res;
   }
smilefr
Posts: 58
Joined: 23 Mar 2009, 16:50
Location: France

Postby smilefr » 10 Jan 2011, 08:30

Thank you, i will give it a try.
janux
Posts: 28
Joined: 21 Sep 2010, 19:11

Postby janux » 12 Jan 2011, 08:17

smilefr wrote:Thank you, i will give it a try.


btw found some thing in sfs2x docs

You could use

com.smartfoxserver.v2.util.MD5

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 48 guests