I'm trying to correctly do a per user and site wide salt for my passwords. Here's what I've got:
require('../../salt.php'); //this is above the web root and provides $salt variable
$pw = mysql_real_escape_string($_POST['pw']);
$per_user_salt = uniqid(mt_rand());
$site_salt = $salt //from salt.php that was required on first line
$combine = $pw . $per_user_salt . $site_salt;
$pw_to_put_in_db = hash("sha512", $combine);
Is this right? Thanks
mysql_real_escape_string
. The resultanthash()
would change any invalid SQL to a letter/number. (2) Ensure you're storing your$per_user_salt
in a safe, secure way to retrieve it when your users are trying to login with their password. – PiercingINSERT INTO users (per_user_salt) VALUES ($per_user_salt)
? – Theorist$per_user_salt
in quotes (single or double) as your hash is a string (i.e.INSERT INTO users (per_user_salt) VALUES ('$per_user_salt')
. – Frankemysql_real_escape_string()
right before the database query and not at the beginning? – Milurdsalt . pw . per_user_salt
then hashing... if you have a better recommendation please let me know... I've never done this before.. – Theoristhash("sha512", $pw . md5($pw) . $site_salt);
, which is about as secure as a random generated number (as long as you don't tell anyone your algorithm). The big upside is, that hacking your database (for example using injection) will not result in theper_user_salt
being shown, as it is notstored
. – Bradberry$combine
to$combine = $pw . $per_user_salt . $site_salt . $site_salt . $per_user_salt . $pw;
that would be more secure...? or is there a simpler way to make it way more secure like$combine = $pw . $per_user_salt . $site_salt . md5($pw);
... – Theorist$combine
becomes more complex, but I like your last option best. However, in the end it comes down to personal preference. Just remember that yoursalt
-trick is a form of security-through-obscurity, the less people know about how you make your$combine
the better. Try to come up with something uncommon, like the two you just posted and you'll be fine. – Bradberry