我生成的盐都完全一样

时间:2014-11-19 15:17:26

标签: php encryption hash salt

所以,我'随机'生成两种盐,用于后来的加密和散列。这些是在应用程序的安装过程中生成的,然后通过以下方式复制到全局配置文件中:

file_put_contents()

现在,当这些生成时,我可以在我的'globalParams.php'文件中查看它们。它们存储为数组的值,但在此安装过程中根本不使用此数组。

生成代码如下:

// Let's generate some encryption salts:

    $options = [
    'cost' => 12,
    'salt' => mcrypt_create_iv(32, MCRYPT_DEV_URANDOM),];

    $salt = password_hash(mt_rand(), PASSWORD_BCRYPT, $options);
    $salt = password_hash($salt, PASSWORD_BCRYPT, $options);

    $salt2 = password_hash(mt_rand(), PASSWORD_BCRYPT, $options);
    $salt2 = password_hash($salt2, PASSWORD_BCRYPT, $options);

在此之后,将它们放入配置文件中,如下所示:

// Let's open up our template globalParams.php and replace some strings..

      $editFile = file_get_contents('newGlobalParams.php');

      $editFile = str_replace( "database_hostname", $hostname, $editFile );
      $editFile = str_replace( "database_username", $dbUser, $editFile );
      $editFile = str_replace( "database_password", $dbPass, $editFile );
      $editFile = str_replace( "database_name", $database, $editFile );

      $editFile = str_replace( "encryption_salt", $salt, $editFile );
      $editFile = str_replace( "encryption_salt2", $salt2, $editFile );

      // Replace the original globalParams.php now that the system is set up..

      file_put_contents('../_includes/globalParams.php', $editFile);

这些是示例输出:

$parameters['main']['salt']   = "$2y$12$cLSGeEoau5/4NEZ3Fe8qquxwUBc6aL5fmcYUlQtavdoIY1L7NKnaG";
$parameters['main']['salt2']   = "$2y$12$cLSGeEoau5/4NEZ3Fe8qquxwUBc6aL5fmcYUlQtavdoIY1L7NKnaG2";

为什么它们相同,但附加了2?

如果需要,可以发布更多代码,包括整个安装程序文件。

的Ta。

修改

以下是在生成后立即回应的结果:

$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA2
$2y$12$uuZoLwioBePD9aDozrOJkuicthSCvq2mpGTQlKNGZ.jLUUrfSDEq.

转储到'globalParams.php'的值:

$parameters['main']['salt']   = "$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA2";
$parameters['main']['salt2']   = "$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA22";

'globalParams.php'的模板:

<?php

// Global configurations file

$parameters['dbC']['hostname']  = "database_hostname";
$parameters['dbC']['username']  = "database_username";
$parameters['dbC']['password']  = "database_password";
$parameters['dbC']['database']  = "database_name";

$parameters['main']['salt']   = "encryption_salt";
$parameters['main']['salt2']   = "encryption_salt2";

session_start(); // Start the session, ready for the user to login with.
putenv( "TZ=Europe/London" ); // Set the timezone for cookies and the sessions.

require_once('databaseFunctions.php');
require_once('coreFunctions.php');

if(file_exists('_install/')) { // Ensures no malicious user can reinstall the application using their own data..

    exit( "Please delete the \"install\" directory." );

}

2 个答案:

答案 0 :(得分:3)

问题在于:

$editFile = str_replace( "encryption_salt", $salt, $editFile );
$editFile = str_replace( "encryption_salt2", $salt2, $editFile );

您正在替换第一次替换时encryption_salt中的encryption_salt2 然后第二次替换什么都不做,因为模式encryption_salt2不再存在。

答案 1 :(得分:0)

Flosculus已经回答了你的问题,但我想指出其他一些细节。

你产生盐的方式&#34;是非常昂贵的。我不确定它们的用途是什么,实际上有四种可能性:

  1. 用作密钥进行加密
  2. 用作 IV 进行加密
  3. 用作密钥/胡椒进行密码哈希
  4. 用作 salt 进行密码哈希
  5. 要散列密码(案例4),最好完全省略salt参数,password_hash()将自动为每个密码生成安全盐。你的服务器cpu对盐使用密钥拉伸是绝对不必要和不好的,相同的盐不应该用于多个密码,并且你放弃了熵以这种方式创建它。

    加密也是如此。如果您需要一个密钥(案例1),那么只需生成一些随机字节并使用bin2hex()将它们存储在配置文件中。如果您需要IV(案例2),则应为要加密的每个文本生成它,并将其与加密字符串一起存储。 IV不应该用于多个加密字符串。