-6

Possible Duplicate:
Generate MD5 hash in Java

MD5 hash was generated using javascript function. Requriement is to generate the MD5 hash generation in java.

On javaScript side, the password was being passed to str_md5() method of Paul Johnston implementation of MD5. How can this be performed in java?

     MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(password.toUpperCase().getBytes());

    byte byteData[] = md.digest();

    //convert the byte to hex format
    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);
    }

   Javascript
    v_password = jQuery.trim(v_password);
v_userid = jQuery.trim(v_userid);
var v_digest = str_md5(v_password.toUpperCase()); // Implementation in java?
var v_pswdDigest = hex_md5(v_digest + v_userid.toUpperCase());
return v_pswdDigest;
  • 3
    You can use a search engine, such as google, to search for how to apply the `md5` hash in Java. Otherwise, please include in your question [what you've tried](http://whathaveyoutried.com). – zzzzBov Jan 24 '13 at 18:52
  • Yes please include what you have tried ? – Chetan Jan 14 '14 at 08:34

1 Answers1

0

The MessageDigest class can be used to generate hashes, including MD5 hashes

Peter Elliott
  • 3,273
  • 16
  • 30
  • How can we obtain the same hash that is being generated using javascript code? If the hash does not match, the user login fails. – user1978406 Jan 24 '13 at 18:52
  • 7
    @user1978406, if you're hashing passwords client side, and using `md5` as your hashing algorithm, you're doing authentication ***very very wrong***. – zzzzBov Jan 24 '13 at 18:55
  • Make sure you're using the same encoding on both sides. UTF-8 is a good rule of thumb. – Louis Wasserman Jan 24 '13 at 19:08