使用与在PHP中创建的python PassLib相同的新密码

时间:2014-08-07 08:59:50

标签: php python passwords crypt

使用crypt()在PHP中生成密码。示例输出是:

$2a$10$F47zZrxfkRN4M4djSBPKXeS5QFdOwSrHn03m40WR1rI8g1cBGHgMO

现在,我要求在Python中使用PassLib生成密码,应该在PHP中验证。

PHP代码:

function generateSalt($cost = 10) {
    if (!is_numeric($cost) || $cost < 4 || $cost > 31) {
        die('Cost parameter must be between 4 and 31.');
    }
    // Get some pseudo-random data from mt_rand().
    $rand = '';
    for ($i = 0; $i < 8; ++$i) {
        $rand.=pack('S', mt_rand(0, 0xffff));
    }
    // Add the microtime for a little more entropy.
    $rand .= microtime();
    // Mix the bits cryptographically.
    $rand = sha1($rand, true);
    // Form the prefix that specifies hash algorithm type and cost parameter.
    $salt = '$2a$' . str_pad((int) $cost, 2, '0', STR_PAD_RIGHT) . '$';
    // Append the random salt string in the required base64 format.
    $salt .= strtr(substr(base64_encode($rand), 0, 22), array('+' => '.'));
    return $salt;
}

crypt('apasswd', generateSalt());
// Output: $2a$10$F47zZrxfkRN4M4djSBPKXeS5QFdOwSrHn03m40WR1rI8g1cBGHgMO

我希望能够使用python生成相同的哈希。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您不能这样做,因为您使用随机生成的盐生成哈希。所以你的最终哈希总是不同的