【问题标题】:New password using python PassLib same as created in PHP使用 python PassLib 的新密码与在 PHP 中创建的相同
【发布时间】:2014-08-07 08:59:50
【问题描述】:

使用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 生成相同的哈希。我该怎么做?

【问题讨论】:

  • @Marc,没有错误。也希望在 Python 中生成相同的密码机制。因此,我可以在 Python 应用程序中验证用户。
  • 对不起,stackoverflow 就像名字提供的那样,是一个支持错误的平台 - 不是免费编码。 python 的 crypt/hashlib 库有据可查。如果你有任何 python 代码,我想帮忙

标签: php python passwords crypt


【解决方案1】:

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

【讨论】:

  • 我不这么认为。因为在验证时我们不通过盐,但是 crypt() 验证。例如:crypt('apasswd', '$2a$10$F47zZrxfkRN4M4djSBPKXeS5QFdOwSrHn03m40WR1rI8g1cBGHgMO') === '$2a$10$F47zZrxfkRN4M4djSBPKXeS5QFdOwSrHn03m40WR1rI8g1cBGHgMO'。所以,盐可以是任何东西!
  • 所以你的问题是错误的,你不想像在 php 中那样生成哈希。您只想生成密码而不生成盐,因为盐已经由 php 生成
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多