The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Reconciling Java and PHP MD5 Hashes
Discuss Reconciling Java and PHP MD5 Hashes in the Java Help forum on Dev Shed. Reconciling Java and PHP MD5 Hashes Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 7th, 2012, 08:17 AM
|
|
|
|
Reconciling Java and PHP MD5 Hashes
I wasn't sure where the right place for this is but since the PHP hash function has little room for silly errors, I decided that the real issue must be in my Java method. I have a hash generated with PHP that gets passed to a Java app. The Java app then tries to reproduce that hash using the same input string so they can be compared for validation purposes. Unfortunately the resultant hashes are different. Here is the relevant code segment:
Code:
MessageDigest sha1=null;
try {
sha1 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sha1.update((memberNumber+" "+serialNumber).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] hash=sha1.digest();
System.out.println(String.format("%0"+hash.length+"x",new BigInteger(hash)));
FWIW, the hash from PHP in is:
2a2527858c8bd2ca6a8ef07c79581bf9
and from Java in hex:
6188c17bba00419cb2c03fc7d3dfd15a
Hopefully there is someone here familiar with both PHP and Java that can see what I am doing wrong.
Also FWIW, here is the PHP hash call:
PHP Code:
$key=hash("MD5",$_POST['membernumber']." ".$result[1]);
I have verified that the strings passed to 'hash' and 'update' are identical. TIA.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
Last edited by gw1500se : December 7th, 2012 at 08:23 AM.
|

December 7th, 2012, 08:50 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
have you compared the exact bytes of each string? This could easily be an encoding problem where the strings look the same but are in fact completely different.
I've tested your Java code and the PHP function with the hard coded string "צה" (encoded with UTF-8), and I do get the same hash.
|

December 7th, 2012, 09:03 AM
|
|
|
|
Thanks for the reply. I guess I am not sure what you mean. Both strings are numeric characters only (with a single intervening space) and the resulting strings look the same. I will double check the lengths to make sure there are no unprintable characters but I doubt that is it.
|

December 7th, 2012, 09:14 AM
|
|
|
Well, I'm red faced.  There was a trailing new line in Java. Thanks for putting me on the right track.
|

December 7th, 2012, 09:27 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
By the way, that BigInteger is problematic, because the byte array is interpreted as a two's complement. If the MD5 hash starts with a 1, you'll get a "random" negative number instead of the actual hexadecimal representation.
You could fix that by simply prepending a 0 to the by array. But I'm pretty sure there are better ways to get the hex representation of a byte array.
|

December 7th, 2012, 01:39 PM
|
|
|
|
It doesn't matter but thanks anyway. That println was for debugging purposes and has been removed.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|