Solved

Java MD5 doesn't match php?

Posted on 2003-12-03
7
1,383 Views
Last Modified: 2008-02-26
Java:
------------------------------------------------------
StringBuffer sb = new StringBuffer();

        try {
            String data = "apple";
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(data.getBytes());
            byte[] digestBytes = messageDigest.digest();

            /* convert to hexstring */
            String hex = null;

            for (int i = 0; i < digestBytes.length; i++) {
                hex = Integer.toHexString(0xFF & digestBytes[i]);

                if (hex.length() < 2) {
                    sb.append("0");
                    sb.append(hex);
                }
                sb.append(hex);
            }
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        System.out.println(sb.toString());

PHP:
------------------------------------------------------
echo md5("apple");
or http://tw.php.net/md5

--
I got a
1f3870be274f6c49b3e31a0cc6728957f (from Java)
1f3870be274f6c49b3e31a0c6728957f  (from PHP)

always differ one bit.

Thanks in advance:)
0
Comment
Question by:YuRen
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9873072
               if (hex.length() < 2) {
                    sb.append("0");
                    sb.append(hex);
                }
                sb.append(hex);

should be:

                if (hex.length() < 2) {
                    sb.append("0");
                }
                sb.append(hex);

You were appending "hex" twice if you found a value less than 0x10 ;-)
0
 

Author Comment

by:YuRen
ID: 9873190
You save my life, jimmack
:)

A few days later, I would implement the security scheme in my web site.
http://www.uwan.com.tw/   Enjoy it!
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9874566
;-)
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Expert Comment

by:kelvinlaw
ID: 10359519
may i ask :

hex = Integer.toHexString(0xFF & digestBytes[i]);

why 0xFF is needed?
0xFF should be 11111111

0xFF & digestBytes[i] same as  digestBytes[i] ??

thanks
0
 

Author Comment

by:YuRen
ID: 10363555
0xff shoud be 00000000 00000000 00000000 11111111

--
        byte[] digestBytes = { (byte) 0x7f, (byte) 0x8f, (byte) 0xff };

        for (int i = 0; i < digestBytes.length; i++) {
            System.out.print(Integer.toHexString(digestBytes[i]) + "\t");
            System.out.print(Integer.toHexString(0xff & digestBytes[i]) + "\t");
        }
--
result:
7f      7f      ffffff8f      8f      ffffffff      ff      
0
 

Expert Comment

by:kelvinlaw
ID: 10364544
1 byte is 8 bits, right?
but
System.out.print(Integer.toBinaryString(digestBytes[i])

if give me a 32 bits answer if digestBytes[i] >= 0x80....


WHY????
0
 

Author Comment

by:YuRen
ID: 10368345
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
thymeleaf natural templating vs JSP 2 95
Java SE 8u111  Lot of stuff broke 11 69
Should localization be done inside spring controller 5 25
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question