Hello,

I hope someone is more knowledgeable in Java than me while still knowing PHP!

The following Java code makes a hash from a string, and I need a PHP equivalent that makes the exact same hashes.
Simply md5() does not do the trick, and I looked at some PHP libraries for BigInteger but maybe it is over-kill to include all sorts of third party libraries?

public static String encrypt( String source ){
    String md5=null;
    try{
        MessageDigest mdEnc = MessageDigest.getInstance("MD5"); // Encryption algorithm
        mdEnc.update(source.getBytes(), 0, source.length());
        md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
    }
    catch(Exception ex){
        return null;
    }
    return md5;
}

Thanks!

Hi, can you paste an output hash? For example, I tried this:

And it returns the same hash of md5() in PHP:

echo md5("hello"); // 5d41402abc4b2a76b9719d911017c592

But I don't know JAVA well, so my example could be wrong.

You're on the ball there cereal. Even though there's some bloat code in that java method and I wanna pull my hair out everytime I see Java devs being lazy by catching all and any exceptions instead of method specific ones, all that method is doing is outputting an md5 hash.

God I love PHP! So simple, yet so beautiful.

for this

mdEnc.update(source.getBytes(), 0, source.length());

not sure if you can get away with it using the PHP function called ord .

Again, I am pretty much speculating here trying to mimic that iteration effects against the length of the input string.

In PHP the lenght of the string can be determine by strlen function. Now, there is a problem with the strlen function in PHP, because all spaces are considered or treated like strings. Therefore " hello + 4 spaces ", will have a strlen value of 10 ( one space before the h and 4 spaces after the o). To alleviate this trouble, PHP offers a solution called trim.

So, now everything are in place. You just need to realize the output of the getBytes() in Java ( I know what it is, I just want you to be resourceful in finding much information about the problem on hand). If the getBytes() returns an ASCII code then, ord is the perfect replacement of it in PHP.

To be able to produce the same results in PHP, you will have to iterate through the input string and process each character with ord.

for example, if we have a "hello_world" string input the ORD output will be somthing like this ( all in ASCII)

Array ( [0] => 104 [1] => 101 [2] => 108 [3] => 108 [4] => 111 [5] => 95 [6] => 119 [7] => 111 [8] => 114 [9] => 108 [10] => 100 ) 

The converted string will then be subject for the md5 hashing.. that's my take on this.

Thanks for all your answers.
I was sure I tested whether the output from the Java code corresponded to PHP's md5() function, but apparantly I did something wrong. Because I see @cereal was right actually.

Sorry for not noticing this myself!

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.