<?php
/*
The following code is written by Morten Rishøj Thomsen.
(c) Ricehigh.dk

This code is distributed freely. If you paid money for it, please claim your money back from the retailer, who sold it to you.
This code may be used freely for private use only.
For any commercial use, please contact me on psychopixi@gmail.com or on ricehigh.dk.
If you are unsure if you're using the code for commercial use, please feel free to ask.

The following meassures are made to prevent a hacker, that has comprimised the database, access to passwords in the database:
 - A provided password will be hashed with a 256-bit protection. The hash algorith used is SHA-256.
 - A random key is appended to the provided password. This way to equal password will be distinct in the database. This way it will be impossible for a potential hacker to grant access to more than one password at a time.
 - A static key is appended to the provided password to prevent rainbow table attacks. This code is only available in the PHP-code, whereas the random key is kept within the database together with the password.

Please note that even though these measures are made very strictly, they don't provide guarentee that your stored passwords are safe. No matter how well a password is hashed it can always be cracked by brute force. This code is intended to make this bruce force as complicated and time demanding as possible, while putting as less strain on the server as possible.
*/


// Defines the length of the salt. The salt may not exceed the lenght of a SHA-256 hashed string (64 characters).  If the salt length is set to zero, no random salt will be used.
// NOTE: it is important that the length of the salt is remained at wanted value for all the time the database will be in use. It will be impossible to compare data, if the salt-length is changed after data is saved.
// NOTE 2: Because the random salt is appended to the hashed string, the salt length changes the space needed in the database. The database should be able to hold 64 characters plus the number of characters defined below.
define('SALT_LENGTH'24);

// Static SALT. You can type one yourself, use a random string, use a hashed string, generate a string using a function similar to the one shown below or generate one at: http://api.wordpress.org/secret-key/1.1/
define('FIXED_SALT''q*x<D:_I7c:IG~O]B5Tv&}-V|DOM(~@z');

function 
generateHash($plainText$salt null){ // The salt variable is set to NULL if no parameter is set.
    
if ($salt === null){ // triple equal signs means "defined as" and is used instead of the comparison (double equal signs) for the instance that the given salt is either zero (0) or an empty string ("").
        // Generates a random string with the defined salt length.
        // Two random functions are used to increase the number of possible outcomes.
        
$salt substr(hash(sha256,(uniqid(rand(), true)) . (uniqid(rand(), true))), 0SALT_LENGTH);
    }
    else{ 
// Uses the supplied string. If the string is too long, it will be shortened to the defined salt length.
        
$salt substr($salt0SALT_LENGTH);
    }
    
// The hash is produced in a matter, so the random salt is appended to the password and the static salt is appended. The final hash is produced by using the SHA-256 algorithm.
    
$hash hash(sha256,$salt $plainText FIXED_SALT);
    return 
$salt $hash// Returns the hashed string with the random salt appended to it
}

/*
The following are made for the sole purpose of testing the code.
$time = microtime(1); // Creates a fingerprint of the current time in micro seconds, for the purpose of testing the speed of the function.
$hash = generateHash("test"); // Calls the generateHash function and stores it in the $hash variable.
$time = microtime(1) - $time; // Creates a new fingerprint, and substracts the previous value.
echo "<b>Time used:</b> " . $time . "<br/><b>Hashed string:</b> " . $hash . "<br/>"; // Outputs the generated hash and the time used to generate it.
*/
?>