0

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
  • You said you don't get the same result with your PHP code. What results do you get when you run that code? You can edit your question to add this information. – Theresa Oct 02 '15 at 14:09
  • And the result with Java? Don't be lazy, provide all the info, we can't play the guessing game here and pulling info from you with a toothpick.. – Mjh Oct 02 '15 at 14:25
  • @Mjh The result in Java is the $result variable in PHP. Now I put it after the PHP code result. – Gianni Retana Oct 02 '15 at 14:29
  • @GianniRetana did you found a solution? Use the bouncycastle library's implementation of Whirlpool and get the same output as PHP's: http://www.cs.berkeley.edu/~jonah/bc/org/bouncycastle/jce/provider/JDKMessageDigest.Whirlpool.html – crubio Nov 12 '15 at 17:24

0 Answers0