0

我用 Java 制作的应用程序使用 Whirlpool 来散列密码。现在我需要对 PHP 使用相同的方法。这是我用 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);
}

但现在我需要将此代码“翻译”为 PHP。这是我试图做的,但我不是使用 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 );

但我没有得到相同的结果。请你帮助我好吗?

编辑:这是我使用实际 php 代码得到的结果:

459897235ff5811026a5393e6ae58706bb99783d62bd58b41f6ca82d978aa17eaccbef95d383f1ac9d68f93f5ba68ef2005c6b467c0fa81977566a708d98e220

这是我使用 Java 得到的结果以及我想要的结果:

8BD01A206E7B5378B00F77A0E3C5C39B7011CCF87F5BE132F495A77F418E0743C6CB514D53EF01CD4A2C170013C8B9C76E03D9CC94BA404983375CBC3E67E703
4

0 回答 0