3raser Posted April 28, 2012 Share Posted April 28, 2012 Lately I've been telling myself to start touching up my security when it comes to passwords, so here I am with another question on PHPFreaks. I've read several salting guides, but I still have a few lingering questions. One of which is: once a salt has been created (see my function below), do I store it in a column named "salt" for each user in the "users" table? It seems like if a hacker got a hold of the database information, they could just ignore the salt and go straight to deciphering a user's hashed password. Just curious about that... Now, onto my simple function I decided to write to give this a try: function generateSalt($username) { //length of salt $char_max = 21; $char_list = array('A', 'B', 'C', 'D', 'G', 'Z', rand(0,200), 9, 8, 6, rand(3,55), rand(7, 1444)); //random numbers and letters will be appended to this variable $gen_chars = ''; for($x = 0; $x < 10; $x++) { $gen_chars .= $char_list[rand(0, count($char_list))]; } //random addition to salt $gen_chars = hash(sha256, $gen_chars); //shorten then hash -- max 5 chars $shorten_user = substr(sha1(strpos($username, 0, 3)), 0, 5); //salt var $salt = $gen_chars.$shorten_user.date('M-d-Y h:m:s'); $salt = substr(hash(sha256, $salt), 0, $char_max); return $salt; } Any feedback regarding this function? I've read that MD5 isn't really reliable, and people should be using SHA256, so I decided to go with that. I also tried to make each user's salt really random and unique. But how does this affect the user's password or make it any securer if I can't combine the salt and password? I know for a fact that I'm missing a piece of information or doing something wrong, so if anyone could help me out: that'd be very appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/261747-first-attempt-at-salting/ Share on other sites More sharing options...
kicken Posted April 28, 2012 Share Posted April 28, 2012 do I store it in a column named "salt" for each user in the "users" table? Yes, you need to store the salt somewhere that way you can use it to generate the hash again when the user logs in. It seems like if a hacker got a hold of the database information, they could just ignore the salt and go straight to deciphering a user's hashed password. Just curious about that... The point of the salt is to make it so that the hacker cannot use a rainbow table to determine what the password might be. By including a salt when you hash the password, you ensure it is unique and won't appear on any such tables. This forces the hacker to re-build any such table using the salt, which is an extremely time consuming and memory hogging process (see this thread for some details). Now, onto my simple function I decided to write to give this a try: Any feedback regarding this function? It's a bit needlessly complex for salt generation. You could just throw together a few random bytes or even just something like the username+time(). Salt's do not need to be complex, just unique. But how does this affect the user's password or make it any securer if I can't combine the salt and password? You do combine the salt and the password, prior to hashing it. Something like: $pass = $_POST['password']; $salt = generateSalt(); $hash = sha1($salt.$pass); Then in your database you store the salt and the hash values. Whenever someone tries to login, you take their username, lookup the salt and hash values, hash their inputted password using the retrieved salt, and see if the hash matches. Something like: $sql = "SELECT salt, hash FROM users WHERE username=$user"; $res = mysql_query($sql); $row = mysql_fetch_array($res); $enteredHash = sha1($row['salt'].$_POST['password']); if ($enteredHash == $row['hash']){ //login valid } else { //login error } Quote Link to comment https://forums.phpfreaks.com/topic/261747-first-attempt-at-salting/#findComment-1341325 Share on other sites More sharing options...
xyph Posted April 28, 2012 Share Posted April 28, 2012 IF you have access to hash_hmac, you can use that to mix the salt/password up for you. On the user comments of the manual page, there's a few posts with user-defined PBKDF2 functions. This is a basic key stretching method - it performs the hash thousands of times to make brute-forcing harder. It's worth reading up on Quote Link to comment https://forums.phpfreaks.com/topic/261747-first-attempt-at-salting/#findComment-1341348 Share on other sites More sharing options...
El Chupacodra Posted April 28, 2012 Share Posted April 28, 2012 I think you overthink your salting. Most of the time you can just make two salt variables with random characters and just put one before and after your password like: $salt1 = mCk9!#g; $salt2 = 5f3!tTuN; $pass = $_POST['password']; //make sure you sanitize this before you accept it - stripslashes and real_escape_string $hashedPass = md5($salt1$pass$salt2); ...and then you put that in your user table. But if your site is going to be anything more than just a secluded place off the internet where old friends meet I suggest you skip both md5 and sha1. Here is a good read from the php.net manual: http://se.php.net/manual/en/faq.passwords.php Quote Link to comment https://forums.phpfreaks.com/topic/261747-first-attempt-at-salting/#findComment-1341364 Share on other sites More sharing options...
xyph Posted April 28, 2012 Share Posted April 28, 2012 If your salt isn't random per password, it's essentially pointless. The article in my signature is, IMO, the better way to go if you want strong hashes. Quote Link to comment https://forums.phpfreaks.com/topic/261747-first-attempt-at-salting/#findComment-1341375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.