Welcome back. So now let's add one more security feature
to our program. And in here,
we are going to include hashing. We're going to hash
are passwords
by making use of the sha
256, the secure hash algorithm
with of course a data size of 256 bits. So
to do this,
we are going to have to import another library
and this one is going to be the hashlib.
OK? So
let me just add a node here and also import for hashing
and there we go. All right.
Now
let us add it at the very top in here the function to actually hash the password. So
let me just add a note function to hash passwords.
OK? So
the name of the function I can say define and we can go with hash password.
And now of course, inside the brackets,
we're going to be working with the password parameter
and then I put my column.
So now I'll take a look at this. OK? To
hash the passwords,
you can simply say return,
OK? And now
the name of the function itself
in the hashlib module
is hashlib.
OK? And now dot
Sha
256.
And now in brackets,
what are we hashing is the password?
And now I'm going to use the method called encode,
open up a new pair of brackets,
close the previous brackets. And now dot
And now finally, we can add a hex
digest,
add closing brackets again. And there it is.
So this is a particular line that you can memorize.
This is the function that will actually uh hash
up passwords by making use of the hashlib module.
So all we need to do right now, very simply
will be to integrate it into the actual registration function. So
let us come all the way down here.
Ok.
And at the very end where the password was successful.
Ok. So here where it says print feedback, return
just before we begin to write the password to the users dot Text file,
let us now hash it.
Ok. So after the return statement,
ok,
I am going to add like a small little notes and we can say hash
uh the password.
Ok.
All right.
So
what we want to do right now is very,
very simply we can create a verbal called hashed underscore password.
And then we can pass the actual password that we've just hash, I'll say hash
underscore password and now in brackets
very simply password.
And there it is.
So all we have to do very simply is where
we write in the password instead of the original password,
we'll now have to write the hashed password. So
here where it says file dot write f username
and now in here instead of password,
we're going to change that to the variable hashed
underscore password.
And there it is, we've successfully added hashing.
So let us see if this will actually work.
I'm gonna go ahead right now and run the program.
Ok. And uh let us register.
So I'm gonna add a new name in here. I'm gonna call this one, let's say David,
OK, password. I'm gonna go with a very strong password in here, sword,
fish,
sword,
fish,
and then 234 at and then asteric
symbol.
And there you go. Ok?
So now let's open up the users or text file and see and wonderful. There you go.
So this right here is the hashed version
of
David's password, which is
Salish 234 at and the Asterik symbol.
So wonderful,
we have added hashing
to our program. It's becoming more and more advanced
and now jump in the very next lesson where we're going to add our audit to feature.
I will see you then.
So I just wanted to give you a quick update.
Yes, we've just added the hashing function.
However, when we test it out, you might notice that it doesn't work.
So if I was to go over here right now, and I wanted to register a new user and I said,
let's call the new user Victor, right?
Victor.
And then
I added a password, Victor
123
and then VIC
and then the add symbol
and I press enter,
it does say that user registration was successful. However,
if I try to log in
as Victor
again,
so I have the username
and now if I type in the password
123
VIC
at
you will see right now that it says invalid username or password.
So for some reason, the login function, it's not able to detect
that the username matches the password.
And this is one of the reasons why you really want to test out your code
whenever you've added new functionality.
So I realized that over here online 60
where it says where we have the login function, it says
that if username equal stored username and password equalto password,
then we can allow the user to successfully log in.
But you can see right now that this isn't going
to work because as the code is written right now,
it is comparing
the password to the plain text format and
not the encrypted version or the hash version.
Rather.
That's why we are having the, the, the the issue.
So we're supposed to come in right now and say start username and,
and now we stress the hash
underscore password
and now in brackets
password.
So this right here, we are now telling
the program and telling Python that hey,
compare
the hashed password of the user and not the plain version.
So it should work right now if I was to go back and let us create a
new account for the user, actually, let's uh
stop the program. Ok?
And I'm gonna run it afresh all over again.
So now let me register a new user and call the user mac and then
uh mac 123 at mac
for the password.
And now let's try to log in as Mac again. So I'm gonna say mac
and now mac 123
at
Mac.
And now you can see the login was in fact successful.
So this is again a reminder that whenever you add new functions and new lines of code,
you want to test before
you begin adding more and more
lines of code to your program. So please
do go ahead and update
the line where it says if username, call to username and password,
you want to make sure that it's the hashed
version of the password that we are comparing. Thank you for watching.
And of course, I will see you in the next class.