Reputation: 39
I have a table in phpmyadmin containing the username and password, but the password field isn't encrypted. I wanted to encrypt the whole column of the password field into SHA1 format. Any idea how to do so?
Upvotes: 0
Views: 8440
Reputation: 922
For today's security standards I would do:
Usage is very simple:
$plain_password="messi";
$hash=create_hash($plain_password);
$hash is an array with all the data you need to save to the database
array(4) {
[0]=>
string(6) "sha256"
[1]=>
string(4) "1000"
[2]=>
string(32) "wJnwu2uA4rVdW8Momz3CgS8W7MdEmaLH"
[3]=>
string(32) "0g2b0ZrpnObAx6z1L/8g8PPNbTG+92BI"
}
Save that to the DB and next time you have to retrieve those value as:
SELECT
CONCAT(password_algo, ":" ,password_iteration,":" , password_salt,":" , password_hash)
FROM users WHERE userEmail='[email protected]';
Use the string result in the validation function
validate_password($password_to_validate, $stored_hash_from_db)
If its valid you will get True, otherwise False.
Upvotes: 0
Reputation: 2712
First, please read How to securely hash passwords?.
Then you can look for a PHP PBKDF2, Bcrypt, or Scrypt implementation to use.
In the database, you'll want:
If you'd really like (and it is NOT recommended), you could create a MySQL implementation of something like PBKDF2, and since MySQL 5.5.5 and up has a SHA-512 function, you can perhaps use a MS SQL Server PBKDF2-HMAC-SHA-512 impelmentation as an example, but be absolutely sure to verify it against known test vectors.
Upvotes: 3
Reputation: 69967
Don't even use SHA-1 hashing alone at this point, it is not considered secure enough by itself.
See the Password Storage Cheat Sheet from OWASP. You should be using a stronger cryptographic function in addition to salting the password with a unique salt for each user.
You can use PHP's password_hash()
function which uses the bcrypt algorithm if you have PHP 5.5, or if you don't have PHP 5.5, you can use the PHP implementation by ircmaxell.
If you used just SHA-1 at this point, you may as well not even bother as most passwords in your database are probably already in an SHA-1 rainbow table.
To update your database, write a short PHP script to read each password from the database, apply the hashing function, and then update that row with the new hashed password. Then modify your register and login functions to use the new hash function when comparing an entered password to one stored in the database.
Upvotes: 1
Reputation: 621
You should be salting your passwords as well: https://crackstation.net/hashing-security.htm
Upvotes: 0
Reputation: 17491
update mytable set password=sha1(password)
you should at least verify if the column definition is able to hold 40 characters, or you can worsen the situation.
I hope you are aware that from then on you should also rewrite your code to encrypt the password before sending and or comparing against the DB.
Upvotes: -1