One of the fundamental concepts of OOP object oriented programming
is something known as encapsulation,
which basically refers to keeping some sort of data
private so that it can only be accessed or modified
in certain ways.
Now,
this is very useful in cybersecurity because it
allows us to protect sensitive data like passwords.
OK?
So
as an example, we could write or create a class and objects
that will store a user's password privately
and then only provide a way to check if the input password is actually correct.
So
let me show you how we can encapsulate data using OOP.
I'm going to keep the exact same class. OK. So class user,
however, I'm going to change the attributive role to password.
OK?
And then obviously in here, I'm going to change self dot role
to password
and then this is gonna be equal to
a password
as well.
However,
the one way to tell Python that, hey,
this particular attribute we want it to be private.
This is in public information, this is private only to the user
and we can do that by going over here
in front of the P A password and then add in
a double underscores.
So this right here, the double underscores will indicate to Python that, hey,
this is a private attribute. We want to encapsulate
the information here.
So
let's now create a method
which we can use to check whether or not
the input provided by the user
actually matches the real password for that user. So what I'm gonna do right now is
I am going to define our method and let's call it authenticate. Ok.
So define authenticate.
And now in brackets,
I'm gonna go with self
and
password.
Ok.
And we're doing this because self here is going to refer to the user whose
password we actually want to check and
then the password itself that we're checking.
Ok.
And now this is the key
we would have to return.
And now self dot
underscore underscore
password
will be now equal to password. So
we want to check
if the password that's stored for the user is going to be
the same as the password that was provided as an input.
That's what this will do right here. Line eight.
So if the password matches
line eight will return true.
But if there isn't a match, it's going to return false. Ok. So now
we need to create our user objects
and make sure that of course, whenever you're creating user objects,
the intention is outside, it should be on the same intention as the class itself.
Ok? Don't
do it inside
the class, but outside it has to match. So over here,
I'm not gonna say
user
equals
and now user,
so let's say we need to provide a username. Uh Let's just go with guest.
Ok, let's just go with guest,
something very simple guest.
And now for the password,
I'm gonna go with capital S and then say sword fish, 123.
Ok.
So this is the password we've created for our user guest. Ok.
Fine. Now
let us try to authenticate the user by using different passwords. Ok.
So
let's try
using the right password and then the wrong password. Ok. So
how are we going to call our method?
I'm gonna say print.
And now in brackets,
user
dot
authenticate.
So we're calling the method right now to authenticate our user.
And in brackets
we want to provide the password we want to check. So
the password in here right now, let me go with the right one is gonna be the same sword
uh fish, 123,
right?
And then
brackets. Ok,
if I were on
the program right now
and oh,
line 12, there's an error message. Oh, we should have an extra bracket in here.
Sorry about that. Let me try it again
and there you go. So now it says true because
the password here,
South Fish 123 is the same as the actual password that we've given to the user.
Now, what I'm gonna do is I'm going to simply copy this line,
OK, copy
paste.
And now let us try using a different password. So
I'm just gonna go capital AB C 123
and OK, that's it. So let me go ahead right now,
we run the program again
and now you can see that it is false because our method
has checked AB C 123 is not equal to saltfish 123.
So
this is how we, we can encapsulate
data
using the object oriented programming.
All we really have to do to indicate that a particular attribute is private,
that we want to encapsulate
is to have the double underscores
in front of the actual attribute name. So
that's it. Thank you for watching the video. I will see you in the next class.