0

How to convert String from input text to md5? I have no clue.

This is part of my .jsp

<tr>
<td><spring:message code="password" text="default text" /></td>
<td>:</td>
<td><input type="password" name="password" required></td>

and this is my controller

@RequestMapping(value="/admin/addUser.html", method=RequestMethod.POST)
public ModelAndView createUserAdmin(@ModelAttribute UserAdmin useradmin, ModelMap model)throws Exception
{
    userService.save(useradmin);
    model.addAttribute("successAdd", "true");
    return listUserAdmin(model);
}

Thanks for any help :)

taringamberini
  • 2,719
  • 21
  • 29
splatter_fadli
  • 761
  • 4
  • 16
  • 32
  • http://stackoverflow.com/questions/415953/generate-md5-hash-in-java – Tarmo Apr 21 '14 at 06:58
  • 1
    I can't help you on spring-hibernate, but if you're trying to hash a password, don't use MD5. MD5 is an old algorithm that is considered flawed or broken (depending on the use scenario) and should not be used in new code. It is also deterministic and very fast to evaluate, making MD5 hashes relatively easy to crack. Use a slow salted hash function like bcrypt or pbkdf2 instead. – user3553031 Apr 21 '14 at 07:03
  • I think, I have a problem when convert string from input text to md5 in my jsp.. any clue?? – splatter_fadli Apr 21 '14 at 07:09

1 Answers1

0

Use this method pass string as parameter it will return MD5 as return string. Store that string in database.

public static String getMD5(String data) throws NoSuchAlgorithmException
    { 
MessageDigest messageDigest=MessageDigest.getInstance("MD5");

        messageDigest.update(data.getBytes());
        byte[] digest=messageDigest.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(Integer.toHexString((int) (b & 0xff)));
        }
        return sb.toString();
    }
subhash lamba
  • 216
  • 1
  • 14