Jump to content

Recommended Posts

 

function hash_password($password, $salt = null) {
// create a salt if not already defined
if (is_null($salt))
	$salt = substr(sha1(uniqid(mt_rand(), true), 0, 10);

// $password will be plaintext at this point

// $site_key should be a large random string statically
// located in a file with secure permissions

$hash = hash_hmac('sha512', $password . $salt, $site_key);

return array('hash' => $hash, 'salt' => $salt);
}

$password = 'abcdef';

$pass = hash_password($password);

 

 

First off I just want to say thank you for the valuable information I have been reading in this topic (for a few days) and I have updated my pages accordingly.

 

My passwords are now salted with a random encrypted string and I am using sha1 but would like to switch to sha512. So I am playing around with some code to learn more about how it works and have noticed that:

 

sha1(test) returns a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

but

sha512(test) just crashes the page.

 

Can anyone help me understand why this is?

Link to comment
https://forums.phpfreaks.com/topic/255336-re-login-script-help/
Share on other sites

OK that makes sense.

 

So if this is used to concatenate the password:

$hash = hash_hmac('sha512', $password . $salt, $site_key);

 

Would this be correct to read it when logging in to the site?

$hash = hash_hmac('sha512', $password . $member['salt'], $site_key);
     			if ($hash == $member['password']) {
 				$id = $member['userid'];
				$_SESSION['userinfo'] = $id;
				session_write_close();
				header("location: somepage.php");
				exit();
  			} else {
				$errorMessage = 'Sorry Your Information is Not Recognized';
			}

 

and where is a good place to store the $site_key?

Yeah, that looks right.

 

Store the site key in a .php file somewhere, like in a config file with your database connection and whatnot. Make sure it's pretty length, like 50-60 characters.

 

Also, the hash returned by this function will be 128 characters long, so that's how much space you need in your password column.

Well I have tried many variations of that script I posted and none seem to work. Not sure why. I echo'd back the $site_key and it is there I checked the DB and the code inserts a 128 char string for the password. I checked all my spelling and such, but no luck. :(

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.