Reputation: 165
I am working on a login frame that checks user input password against the password in a SQL database. I convert the text into an MD5 to store in the database with the following
HASHBYTES('MD5', 'JavaTest')
And that produces 5E58D71FBD6D17577AAAB8711264A813.
Then in java I use the following code to attempt to convert the same password "JavaTest" into MD5 to compare against.
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(password.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
hashText = bigInt.toString();
But that produces the string 150157350912923000195076232128194914284
What am I doing wrong?
EDIT: I do not believe this is a duplicate because I have researched answers and it has gotten me this far but I can not figure out what I am doing wrong.
Upvotes: 0
Views: 485
Reputation: 1354
Just pass radix parameter to bigInt.toString
. If you need hex representation pass 16 as radix like this:
hashText = bigInt.toString(16);
public String toString(int radix)
Returns the String representation of this BigInteger in the given radix. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString). The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate. (This representation is compatible with the (String, int) constructor.)
Parameters:
radix - radix of the String representation. Returns: String representation of this BigInteger in the given radix.
Also, you can build hex string form digest byte array without BigInteger like this:
public static String bytesToHex(byte[] bytes) {
StringBuilder builder = new StringBuilder();
for(byte b : bytes) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
Upvotes: 1