Java Whirlpool to PHP

159 Views Asked by At

My application made in Java uses Whirlpool to hash a password. Now I need to use the same method with PHP. This is the code I've made with Java:

private String getPasswordHash(String user, String password)
{
    String salt = "M0z3ah4SKieNwBboZ94URhIdDbgTNT";
    String user_lowercase = user.toLowerCase();

    // mix the user with the salt to create a unique salt
    String uniqueSalt = "";
    for(int i = 0; i < user_lowercase.length(); i++)
    {
        uniqueSalt += user_lowercase.substring(i, i + 1) + salt.substring(i, i + 1);
        // last iteration, add remaining salt to the end
        if(i == user_lowercase.length() - 1)
            uniqueSalt += salt.substring(i + 1);
    }

    Whirlpool hasher = new Whirlpool();
    hasher.NESSIEinit();

    // add plaintext password with salt to hasher
    hasher.NESSIEadd(password + uniqueSalt);

    // create array to hold the hashed bytes
    byte[] hashed = new byte[64];

    // run the hash
    hasher.NESSIEfinalize(hashed);

    // turn the byte array into a hexstring
    char[] val = new char[2 * hashed.length];
    String hex = "0123456789ABCDEF";
    for(int i = 0; i < hashed.length; i++)
    {
        int b = hashed[i] & 0xff;
        val[2 * i] = hex.charAt(b >>> 4);
        val[2 * i + 1] = hex.charAt(b & 15);
    }
    return String.valueOf(val);
}

But now I need to "translate" this code to PHP. This is what I've tried to do, but I'm not that expert using PHP:

$salt = "M0z3ah4SKieNwBboZ94URhIdDbgTNT";
$user = "Speedys";
$password = "test123";

//This is what the final result should look like:
$result = "8BD01A206E7B5378B00F77A0E3C5C39B7011CCF87F5BE132F495A77F418E0743C6CB514D53EF01CD4A2C170013C8B9C76E03D9CC94BA404983375CBC3E67E703";

$user_lowercase = strtolower($user);

$uniqueSalt = "";

        for($i = 0; $i < strlen($user_lowercase); $i++)
        {
            $uniqueSalt .= substr($user_lowercase, $i, $i + 1) . substr($salt, $i, $i + 1);
            // last iteration, add remaining salt to the end
            if($i == strlen($user_lowercase) - 1)
                $uniqueSalt .= substr($salt, $i + 1);
        }

$hash = hash( 'whirlpool', $password.$uniqueSalt );

But I don't get the same result. Could you please help me?

EDIT: This is the result I've got with the actual php code:

459897235ff5811026a5393e6ae58706bb99783d62bd58b41f6ca82d978aa17eaccbef95d383f1ac9d68f93f5ba68ef2005c6b467c0fa81977566a708d98e220

This is the result I get with Java and what I want to have:

8BD01A206E7B5378B00F77A0E3C5C39B7011CCF87F5BE132F495A77F418E0743C6CB514D53EF01CD4A2C170013C8B9C76E03D9CC94BA404983375CBC3E67E703
0

There are 0 best solutions below