File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes MD5 Digest Problem in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "MD5 Digest Problem in Java" Watch "MD5 Digest Problem in Java" New topic
Author

MD5 Digest Problem in Java

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Dear Folks,
I am no good in data structure and algorithm specially due to very rich collection api of java. But now-a-days I am working on a few projects that requires language independent solutions to DSA problems. I was thinking to attend an online training course on that...short (may be in C++) but effective so that after the course I can carry on myself. If you know a few links, please let me know (I tried google and got so many links that made me lost in forest).
One of my recently developer java xmlrpc client works fine that interacts with an xmlrpc server developed in python. Now I need to do md5 checksum of my sent data to the server that also checks whether data was modified during transport. But the data I get after digest is not matching with the data produced as a result of MD5 digesting by my python server. As I know, MD5 is a non-reversable(or almost?!) two-step way to generate a checksum value that should generate the same checksum every time the same input is given to digest. Then as my one is not matching, I am a little worried where am I making the mistake? The current digest is accepted by the python server but that doesn't that help me.
I tested generating though python's code the "Hello World" string and got this following digest - b10a8db164e0754105b7a99be72e3fe5

But when I print it in Java, it gets me.
I tried it in PHP to see if "Hello World" gives the same value. Yes, the following code generates the same.


The following methods are used in one of my utility class.

The value I get are -
������*$���v
or when padded the bytes
ffffffb3ffffff8bffffffe257f99ffffff92ffffff88fffffff62a24ffffffe1ffffff8affffffc476

The problem is XMLRPC sends good XML content via HTTP and so it can't send this sort of bytes-looking data, this contradicts with the XML encoding while trying to wrap up over the wire.
Then I tried preparing padHexString() (with hope to make single byte 0-9 as 00-09) and returnHexString() (with hope I'll get something out of Integer.toHexString) seperately. combinedly etc in various orders, but could not generate the b10a8db164e0754105b7a99be72e3fe5 digest in java. Probabvly I need to know more on MD5
Can you help me through some comments? My idea is just to generate b10a8db164e0754105b7a99be72e3fe5 from "Hello World" using MD5 digest in Java.
[fixed the width of the code example - Ilja]
[ May 11, 2004: Message edited by: Ilja Preuss ]

Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
One thing to be very careful about here is the use of Strings. MD5 takes a sequence of bytes as input and returns a sequence of bytes as output. There may be descrepencies in how the Java and Python versions convert strings to byte arrays. Also, note this line:
sReturnStr = new String( md5.digest() );
That's asking for trouble. Your binary is being converted into a string using whatever the local encoding may be. Converting random binary bits to Strings will usually have unexpected results (if it works at all). If you convert your code to think in terms of byte arrays instead of strings and to ensure that the conversion between strings and byte arrays behaves the same in both the python and java versions, that may fix your problem.
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Thank you very much David for the hint. While I was thinking with the StringBuffer, my friend Mojahed sent me a sample code in Java that I am providing here for everyone's sake. This code works fine!

Some tasks which are childishly simple in other languages, are made
unnecessarily(?!) complex in Java. If you don't agree, see the 3 line python code and 1 line PHP code that I posted at the very top.
Iv�n J�zsef Bal�zs
Greenhorn

Joined: Feb 24, 2004
Posts: 2
You wrote:
<CITE>
Some tasks which are childishly simple in other languages, are made
unnecessarily(?!) complex in Java. If you don't agree, see the 3 line python code and 1 line PHP code that I posted at the very top.
</CITE>
Well, computing an MD5 digest is intentionally and inherently difficult. Otherwise it could be a usable digest. You can have an easy coding in Java too, if relying upon a given infrastructure:
private final static String DIGEST_ALGO = "MD5";
String PasswordDigest = org.apache.catalina.realm.RealmBase.Digest(user_pass,DIGEST_ALGO);
Corneil du Plessis
Greenhorn

Joined: Feb 12, 2004
Posts: 13
It seems to me you need to look at the XML-Signature specification
Jerry Horton
Greenhorn

Joined: Mar 31, 2004
Posts: 2
Originally posted by Corneil du Plessis:
It seems to me you need to look at the XML-Signature specification

I took the digest byte value and applied bas64 encoding to make the bytes "string safe"
leo mari
Greenhorn

Joined: Nov 12, 2003
Posts: 15
Take a look of Jakarta Commons
http://jakarta.apache.org/commons/codec/

....
static String md5Hex(byte[] data)
static byte[] md5(String data)
static String md5Hex(String data)
....

System.out.println(DigestUtils.md5Hex("Hello World")); //b10a8db164e0754105b7a99be72e3fe5
Bye
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
This thread seems to be more appropriate for the Security forum - moving ...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: MD5 Digest Problem in Java