Avatar of isames

asked on 

Java: password entered is not converting properly

So my instructor gave us this code to convert the password entered by the user to a series of letters and numbers. For example, if I pass in "alphabet soup" , then it should return 108de81c31bf9c622f76876b74e9285f

To make sure I'm passing in the correct password, which is "alphabet soup" , I tested returning the variables original and userpassword. They both displayed "alphabet soup"
however when I returned the sb.toString(); I get 6cfa0a0071bb0f00ad9db9f5b8240d61 instead of 108de81c31bf9c622f76876b74e9285f

Also, if I manually type "alphabet soup" into the code below for the original variable, sb.toString(); returns the correct value of 108de81c31bf9c622f76876b74e9285f.

Why am I getting the wrong value when I pass the value in via the parameter, even though everything I see seems to be right?

public static String MD5Digest(String userPassword)throws Exception{
        String original = userPassword;  //Replace "password" with the actual password inputted by the user
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
            //System.out.println("original:" + userPassword);
            //System.out.println("digested:" + sb.toString());
        return sb.toString(); 
        //return userPassword;
        //return original;

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon