Hashed passwords do not match
807603Jan 6 2008 — edited Jan 6 2008Hi everyone,
I'm building a database application that requires authentication in the shape of usernames and passwords. I've successfully implemented a graphical login client (so that entered passwords are 'starred out') and this works with normal String passwords. However, using Java's integrated MD5 engine to hash the passwords, something is not matching. No matter what I have tried, an MD5-hashed password will not be accepted. I am very sure the password is correct. The database is based on Random Access Files and, on authority of one of my friends, MD5 hashes are 16 bytes long, so my save file is set up to write 16 bytes of data. Printing out the passowrds to the command prompt shows them to match on both the entered word (when hashed) and the hashed password read out of the database. However, probably due to the character sets, the Command Prompt string looks nothing like the string that is written to the file.
I've used a very large amoutn of code in my program, so to start off with, I'll post my encryption method:
public static String hash(String input)
{
byte[] unhashed = input.getBytes();
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hashed = md.digest(unhashed);
String output = new String(hashed);
return output;
}
catch(Exception e)
{
System.out.println("Error in hashing operation: ");
e.printStackTrace();
return null;
}
Both the login client and the user creation method use this method to hash the passwords. My login method itself is a pretty standard Random Access File that does a direct comparison between two strings (i.e. string1.equals(string2)). I'm really lost with why this doesn't work. Encryption would be a big bonus to my work, but I can't for the life of me work out where I've gone wrong. I'll post more code if it's needed, but in the mean time, any help would be greatly appreciated!
Rob Johnson