【发布时间】: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