Click here to Skip to main content
15,883,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i implemented this code to hash user password, but when i try to hash the same password again, it generates another hash!
this is my code:
Java
public static String ConvertToHash(String password)
	{
		try {
			MessageDigest ms = MessageDigest.getInstance("MD5");
			byte[] pass = password.getBytes();
			byte[] hashedpass = ms.digest(password.getBytes("UTF-8"));
			String hashed = hashedpass.toString();
			return hashed;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}

what is the error causes that?
thanks in advance!
Posted
Updated 23-Jan-15 10:36am
v2
Comments
Sergey Alexandrovich Kryukov 23-Jan-15 19:01pm    
Don't! This hash function has been broken, not suitable for security purposes. Same thing about SHA-1. Use form from SHA-2 family, or even newest SHA-3.
—SA

This one works:

import java.security.MessageDigest;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

public class HelloWorld{
    public static String ConvertToHash(String password)
	{
		try {
			MessageDigest ms = MessageDigest.getInstance("MD5");
			byte[] pass = password.getBytes();
			byte[] byteData = ms.digest(password.getBytes("UTF-8"));
			StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
             sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			return null;
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
     
     public static void main(String []args){
        System.out.println(ConvertToHash("password"));
        System.out.println(ConvertToHash("password"));
     }
}
 
Share this answer
 
v2
Comments
Salah Abualrob 23-Jan-15 16:54pm    
i know it is not reversible, but i know usually if you want to store a password you must hash it then store it .. but if the result is not the same if i hash it again, how can be implemented?
Zoltán Zörgő 23-Jan-15 16:58pm    
Ok, I missunderstood your post. Let me check.
Zoltán Zörgő 23-Jan-15 17:19pm    
See update
Salah Abualrob 23-Jan-15 17:27pm    
you solved it for me, thanxx so much :)
Sergey Alexandrovich Kryukov 23-Jan-15 19:04pm    
One big problem: don't recommend MD5, it should not be used for security purposes. Please see my comment to the question and answer. MD5 was found broken.
—SA
Please see my comment to the question: don't use MD5! For example, you can use SHA-256 through the class java.security.MessageDigest:
http://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html[^].

—SA
 
Share this answer
 
Comments
Salah Abualrob 24-Jan-15 7:40am    
I changed it to SHA-256
thank you :)
Sergey Alexandrovich Kryukov 24-Jan-15 12:04pm    
Great. Will you accept this answer formally as well?
—SA
Salah Abualrob 24-Jan-15 14:55pm    
surely :)
Sergey Alexandrovich Kryukov 24-Jan-15 15:18pm    
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900