Let's wrap up our password class in this video.
We're going to implement these static compare method right here.
So we're going to take some stored password out of our database and some supplied password as well.
And going backward to the diagram we just looking at.
Remember we're going to hash the supplied password get the hash version.
Well then compared to the one stored inside of our database and if they are equal that means that the
user must have given us the correct password something that's just kind of complicating this a little
bit.
The stored password right here is whatever we are storing tied to our user inside the database.
So it's going to be the output of two hash as a part of that two hash process.
We are not only generating a hash password but we are also concatenated on there at the very end.
Separated by that little dot something called the salt as well the salt is part of the actual hashing
process.
So start password right here is not just the hash password.
It's not just that part.
It is the hash password plus a period.
And the salt at the very end so we're going to accommodate for that on the very first line by saying
hash password and salt are coming from stored password that splits on dot like so.
So now hash password is the actual truly hash password that is stored in our database.
And salt is the salt that we generated during the initial hashing process.
I know this stuff is confusing again really assuming that you've seen how password hashing and salting
and all this stuff at least once before.
So after that we're going to go ahead and go through the same exact password hashing process on the
supplied password do we are going to write out the same line as up here rather than retype yet.
I'm just going to copy that and paste it.
I am going to change password right there to the actual argument name of supply password.
I'm gonna make sure I also mark this function as async and then finally we now have the original hash
password plus a buffer containing the newly hash password the one the user just supplied.
They're going to take the buffer turn it into a string and then compare it against the hash one language
to return buff dot to string converted to a hex hexadecimal string I'm going to compare its equality
verse the stored hashed password like some and that is it.
So we've got our class password all put together now we've got the ability to hash a password and we've
got the ability to compare two as well.
So the last we have to do is go back over to our user model.
We're going to import that password manager class we just put together we're going to wipe it up inside
of this user model to make sure that we automatically hash a password whenever we attempt to save a
user to our database.