So, I'm 'randomly' generating two salts for use with later encryption and hashing. These are generated during the application's install process and then copied into a global configurations file via:
file_put_contents()
Now, when these are generated, I can view them in my 'globalParams.php' file. They are stored as values of an array, but this array is not utilised at all in this installation process.
The code for generation is as follows:
// 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);
After this, they are placed into the config file like so:
// 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);
And these are example outputs:
$parameters['main']['salt'] = "$2y$12$cLSGeEoau5/4NEZ3Fe8qquxwUBc6aL5fmcYUlQtavdoIY1L7NKnaG";
$parameters['main']['salt2'] = "$2y$12$cLSGeEoau5/4NEZ3Fe8qquxwUBc6aL5fmcYUlQtavdoIY1L7NKnaG2";
Why are they identical, but with an appended 2?
More code, including the entire installer file, can be posted if needed..
Ta.
Edit:
Here are the results that are echoed right after generation:
$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA2
$2y$12$uuZoLwioBePD9aDozrOJkuicthSCvq2mpGTQlKNGZ.jLUUrfSDEq.
Values dumped to 'globalParams.php':
$parameters['main']['salt'] = "$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA2";
$parameters['main']['salt2'] = "$2y$12$uuZoLwioBePD9aDozrOJkus3e/DuShspaqKzzCDVne6BwVsyDkBA22";
Template of '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." );
}
The problem is this:
You are replacing the
encryption_salt
inencryption_salt2
on the first replacement.Then the second replacement does nothing because the pattern
encryption_salt2
no longer exists.