Jump to content

First attempt at salting


3raser

Recommended Posts

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.

 

:)

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.