I need to know how the mechanisms of hash() o password_hash()
I am making an application and I want to encrypt it so that it is stored in the database and when I want to compare that string entered in a login it does not throw me an error. For example
$pass="pepito";
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
insert into tabla (password) values ($hashed_pass);
And when I go to consult, return the value, but decrypt the value that was sent encrypted
For example
$pass="pepito";
select password from tabla where password = $pass;
Obviously it will throw me an error because before I have to pass the password to the hash algorithm, however I did some tests and every time I haveh it, it totally changes the algorithm, that prevents me from comparing the password in the database and bringing me the values
Help, thanks.
I am going to leave you an example of how to save the password with a secure method
password_hash()
, I would not apply amd5
single one, since there are pages where we can easily decrypt passwordsmd5
, for example.http://md5cracker.org/
Let's see how it works
password_hash()
, it creates a new password hash using a strong one-way hash algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created with crypt() can be used with password_hash().Now we see how we can verify the inserted password with
hash_equals
.hash_equals
— Secure string comparison against timing attacksIt is really not recommended to save a password that can be decrypted so easily. If in the end the password can be cracked by a common method, what is the point of keeping it encrypted?
What is usually done is to save the encrypted password (with MD5 or SHA1 for example) and then when making a comparison, the string to be compared is written and compared with the encrypted password that has been stored in the database. You can do this encryption at the PHP or MySQL level using the MD5 or SHA1 functions. I leave you a link where they talk about this topic at the end: http://blog.aulaformativa.com/consultorio-desarrollo-web/
Like the password, it encrypts
password_hash
it with and returns a string like:There is a function to compare if a hash corresponds to a password that is entered in plain text.
password_verify
returnsTRUE
orFALSE
.Good morning.
What I do to encrypt the passwords in saving them in MD5, I use:
$password = md5($_POST['password'])
and to insert I do:insert into tabla values ($password);
To make the query, I would
With what they told me I was able to put together this code:
I entered the passwords already hashed, they are already like this in the database, I made this code:
Now I want to compare the passwords, like for example a
2000000
. I put together the code like this:But I have not been able to do it, it returns me that "it is not the same", what did I do wrong?