• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MD5 Digest Problem in Java

 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me you need to look at the XML-Signature specification
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread seems to be more appropriate for the Security forum - moving ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic