So, I found out on SO that you're supposed to hash the password together with a "salt". (The articles can be found here and here.)
Here's the code:
$password = 'fish';
/* should be "unique" for every user? */
$salt= 'ABC09';
$site_key = 'static_site_key';
hash_hmac('sha1', $password . $salt, $site_key);
And now I need to save both the $password
and $salt
in MySQL, like so:
+---------+--------+----------+-------+
| user_id | name | password | salt |
+---------+--------+----------+-------+
| 1 | krysis | fish** | ABC09 |
+---------+--------+----------+-------+
** fish
will of course be hashed and not stored in plain text.
And I'm just wondering whether or not it actually makes sense to do it this way, because this way a hacker or whoever will also know the salt? So, if they crack the password and the see it's fishABC09
they automatically will know the password is fish
? Or might he "never" be able to crack the password because he doesn't know the secret_key
, as it isn't stored in the database?
I'm sorry if I'm not making any sense. I just always used sha1
for passwords, and today I found these articles that talked about adding a salt
.
A salt is a random number of a fixed length. This salt must be different for each stored entry. It must be stored as clear text next to the hashed password.
From
https://www.owasp.org/index.php/Hashing_Java#Why_add_salt_.3F
Don't use SHA1, it is no longer secure.
I suggest doing all hashing in MySQL, that way you can be sure there's no difference in the outcome of the hash.
Select a user using:
No, because when the password gets hashed it doesn't look like
fishABC09
, it looks like:5f4dcc3b5aa765d61d8327deb882cf99
which is an md5 hash.To gain access to your system, even if they know the hash, it cannot be reversed. We use salts in order to add complexity to our hashes, and to prevent hash lookups in rainbow tables.
For example: Do a Google search for the md5 hash of "password" which is:
5f4dcc3b5aa765d61d8327deb882cf99
A lot of results right?
Now I'm going to create a hash again, I will still use "password", but I will add a SALT which is "AHG(*@". I'm guessing the only response will be for this post and some bot scrapers that have read this post :)
Should be only a few results, or this post which are this post.
Just Remember
Just remember this... hashes are one way, so even if you gain the hash, you do not know what was used to create it
I know this is old, but for anyone that manages to stumble on this post...
What you are really trying to do HMAC. Trying to do that yourself creates issues. You can partially compute hashes, which reduces the amount of effort required to guess at a password, for instance. HMAC addresses those kinds of concerns.
Better still is scrypt or bcrypt. HMAC still often uses hash algorithms that are designed to be quick and easy to compute; there is even hardware implementations of many of the hash algorithms. bcrypt is computationally expensive and scrypt is memory intensive. Both make things harder for an attacker, but scrypt in particular makes it really hard to build hardware devices to crack a password.
I really like the chart over here: https://github.com/pbhogan/scrypt#why-you-should-use-scrypt
its an old topic but others will come here too so i will try to describe it very easy:
if you do hash(password) you get the same hashvalue for every password [hash(password) = hash(password)]. if two users have the same password, you will see it because the hashvalues are the same. some passwords like "password" or "12345678" are taken very often so: same hashvalue in your database -> maybe password "password" or "12345678" (rainbowtable attack).
if you hash(salt+password) you dont get the same hash for the same passwords because hash(salt1+password) is not hash(salt2+password).
hash(x) is just a mathematical function like f(x)=y. if you put the same x you will get the same y. this function must be "special" to be safe. just dont use sha1 because it is not safe anymore :D
If a hacker gets access to your PHP files, he can simply add mail function, so whoever login, account details are emailed to hacker.
If hacker only gets access to database, he should not get passwords plainly written there, so crypt it before saving. Save it in md5 hash which can't be reversed.
I normally use salt based on username or userID, that PHP program know how to generate for each user along with static_site_key.