【问题标题】:My generated salts are both exactly the same我生成的盐都完全相同
【发布时间】:2015-01-17 04:19:11
【问题描述】:

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

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." );

}

【问题讨论】:

  • 您将值放在 $parameters 数组的什么位置?
  • 在将值转储到文件之前对其进行回显。还向我们展示您将它们假脱机到的文件的模板。
  • 在哪里?在第二个代码块中。它们只是用原来的占位符@mkaatman 换掉了
  • 在编辑中添加了更多细节,@Flosculus

标签: php encryption hash salt


【解决方案1】:

问题是这样的:

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

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

【讨论】:

  • 在您发帖之前我已经将其更改为“encryption2_salt”,因为我认为这可能是问题所在,但我仍然遇到同样的行为。编辑:从头开始,我更改后多了2个。删除它并再次保存文件对其进行排序。谢谢!
【解决方案2】:

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

生成“盐”的方式非常昂贵。我不确定它们的目的是什么,实际上有四种可能:

  1. 用作密钥进行加密
  2. 用作IV进行加密
  3. 用作密码散列的key/pepper
  4. 用作密码散列的salt

要散列密码(情况 4)最好完全省略 salt 参数,password_hash() 会自动为每个密码生成一个安全的 salt。对于您的服务器 cpu 来说,对盐使用密钥拉伸是绝对没有必要和不好的,相同的盐不应该用于多个密码,并且您以这种方式创建它会丢失熵。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多