1
00:00:00,460 --> 00:00:05,559
One of the fundamental concepts of OOP object oriented programming

2
00:00:05,980 --> 00:00:08,600
is something known as encapsulation,

3
00:00:08,779 --> 00:00:11,640
which basically refers to keeping some sort of data

4
00:00:11,649 --> 00:00:15,439
private so that it can only be accessed or modified

5
00:00:15,789 --> 00:00:17,049
in certain ways.

6
00:00:17,200 --> 00:00:17,540
Now,

7
00:00:17,549 --> 00:00:19,760
this is very useful in cybersecurity because it

8
00:00:19,770 --> 00:00:22,649
allows us to protect sensitive data like passwords.

9
00:00:22,659 --> 00:00:23,079
OK?

10
00:00:23,450 --> 00:00:24,000
So

11
00:00:24,409 --> 00:00:28,829
as an example, we could write or create a class and objects

12
00:00:29,030 --> 00:00:31,840
that will store a user's password privately

13
00:00:32,090 --> 00:00:38,549
and then only provide a way to check if the input password is actually correct.

14
00:00:38,889 --> 00:00:39,389
So

15
00:00:39,599 --> 00:00:44,500
let me show you how we can encapsulate data using OOP.

16
00:00:44,689 --> 00:00:48,909
I'm going to keep the exact same class. OK. So class user,

17
00:00:49,240 --> 00:00:54,180
however, I'm going to change the attributive role to password.

18
00:00:54,889 --> 00:00:55,549
OK?

19
00:00:55,770 --> 00:00:59,189
And then obviously in here, I'm going to change self dot role

20
00:00:59,580 --> 00:01:01,389
to password

21
00:01:02,069 --> 00:01:04,410
and then this is gonna be equal to

22
00:01:05,589 --> 00:01:07,010
a password

23
00:01:07,540 --> 00:01:08,370
as well.

24
00:01:08,650 --> 00:01:09,730
However,

25
00:01:10,300 --> 00:01:14,010
the one way to tell Python that, hey,

26
00:01:14,319 --> 00:01:19,410
this particular attribute we want it to be private.

27
00:01:19,660 --> 00:01:23,690
This is in public information, this is private only to the user

28
00:01:24,040 --> 00:01:26,769
and we can do that by going over here

29
00:01:26,989 --> 00:01:29,489
in front of the P A password and then add in

30
00:01:29,879 --> 00:01:32,120
a double underscores.

31
00:01:32,300 --> 00:01:37,709
So this right here, the double underscores will indicate to Python that, hey,

32
00:01:37,819 --> 00:01:42,900
this is a private attribute. We want to encapsulate

33
00:01:43,080 --> 00:01:44,660
the information here.

34
00:01:44,989 --> 00:01:45,580
So

35
00:01:45,809 --> 00:01:48,180
let's now create a method

36
00:01:48,419 --> 00:01:51,220
which we can use to check whether or not

37
00:01:51,660 --> 00:01:54,029
the input provided by the user

38
00:01:54,160 --> 00:02:00,209
actually matches the real password for that user. So what I'm gonna do right now is

39
00:02:00,750 --> 00:02:04,680
I am going to define our method and let's call it authenticate. Ok.

40
00:02:05,059 --> 00:02:06,720
So define authenticate.

41
00:02:07,150 --> 00:02:08,479
And now in brackets,

42
00:02:08,830 --> 00:02:10,809
I'm gonna go with self

43
00:02:11,589 --> 00:02:12,529
and

44
00:02:12,839 --> 00:02:14,130
password.

45
00:02:14,570 --> 00:02:15,149
Ok.

46
00:02:15,509 --> 00:02:20,529
And we're doing this because self here is going to refer to the user whose

47
00:02:20,539 --> 00:02:23,080
password we actually want to check and

48
00:02:23,089 --> 00:02:25,949
then the password itself that we're checking.

49
00:02:26,240 --> 00:02:26,779
Ok.

50
00:02:27,149 --> 00:02:29,580
And now this is the key

51
00:02:29,970 --> 00:02:32,570
we would have to return.

52
00:02:33,270 --> 00:02:35,830
And now self dot

53
00:02:36,080 --> 00:02:37,309
underscore underscore

54
00:02:37,539 --> 00:02:38,770
password

55
00:02:39,289 --> 00:02:43,679
will be now equal to password. So

56
00:02:43,869 --> 00:02:45,600
we want to check

57
00:02:45,850 --> 00:02:49,479
if the password that's stored for the user is going to be

58
00:02:49,490 --> 00:02:53,789
the same as the password that was provided as an input.

59
00:02:53,940 --> 00:02:56,139
That's what this will do right here. Line eight.

60
00:02:56,339 --> 00:02:58,419
So if the password matches

61
00:02:58,679 --> 00:03:01,080
line eight will return true.

62
00:03:01,300 --> 00:03:06,949
But if there isn't a match, it's going to return false. Ok. So now

63
00:03:07,550 --> 00:03:11,130
we need to create our user objects

64
00:03:11,389 --> 00:03:14,520
and make sure that of course, whenever you're creating user objects,

65
00:03:14,529 --> 00:03:19,960
the intention is outside, it should be on the same intention as the class itself.

66
00:03:19,970 --> 00:03:20,669
Ok? Don't

67
00:03:20,830 --> 00:03:22,130
do it inside

68
00:03:22,380 --> 00:03:26,130
the class, but outside it has to match. So over here,

69
00:03:26,410 --> 00:03:27,539
I'm not gonna say

70
00:03:28,279 --> 00:03:29,339
user

71
00:03:29,949 --> 00:03:30,910
equals

72
00:03:31,190 --> 00:03:32,610
and now user,

73
00:03:33,570 --> 00:03:38,899
so let's say we need to provide a username. Uh Let's just go with guest.

74
00:03:38,910 --> 00:03:40,389
Ok, let's just go with guest,

75
00:03:40,750 --> 00:03:42,389
something very simple guest.

76
00:03:43,080 --> 00:03:45,449
And now for the password,

77
00:03:45,899 --> 00:03:51,479
I'm gonna go with capital S and then say sword fish, 123.

78
00:03:51,740 --> 00:03:52,259
Ok.

79
00:03:52,479 --> 00:03:56,619
So this is the password we've created for our user guest. Ok.

80
00:03:57,000 --> 00:03:58,610
Fine. Now

81
00:03:59,320 --> 00:04:04,009
let us try to authenticate the user by using different passwords. Ok.

82
00:04:04,289 --> 00:04:04,929
So

83
00:04:05,440 --> 00:04:06,699
let's try

84
00:04:06,990 --> 00:04:10,759
using the right password and then the wrong password. Ok. So

85
00:04:10,860 --> 00:04:13,580
how are we going to call our method?

86
00:04:13,990 --> 00:04:15,580
I'm gonna say print.

87
00:04:16,209 --> 00:04:17,829
And now in brackets,

88
00:04:18,608 --> 00:04:20,029
user

89
00:04:20,670 --> 00:04:21,459
dot

90
00:04:21,850 --> 00:04:23,350
authenticate.

91
00:04:23,730 --> 00:04:26,750
So we're calling the method right now to authenticate our user.

92
00:04:27,100 --> 00:04:28,339
And in brackets

93
00:04:28,579 --> 00:04:32,000
we want to provide the password we want to check. So

94
00:04:32,179 --> 00:04:36,220
the password in here right now, let me go with the right one is gonna be the same sword

95
00:04:36,450 --> 00:04:38,390
uh fish, 123,

96
00:04:39,320 --> 00:04:39,769
right?

97
00:04:40,619 --> 00:04:41,440
And then

98
00:04:43,079 --> 00:04:44,339
brackets. Ok,

99
00:04:45,040 --> 00:04:46,260
if I were on

100
00:04:47,019 --> 00:04:48,299
the program right now

101
00:04:48,920 --> 00:04:50,059
and oh,

102
00:04:50,160 --> 00:04:55,079
line 12, there's an error message. Oh, we should have an extra bracket in here.

103
00:04:55,820 --> 00:04:57,730
Sorry about that. Let me try it again

104
00:04:58,119 --> 00:05:00,799
and there you go. So now it says true because

105
00:05:01,119 --> 00:05:02,000
the password here,

106
00:05:02,010 --> 00:05:07,500
South Fish 123 is the same as the actual password that we've given to the user.

107
00:05:07,730 --> 00:05:10,839
Now, what I'm gonna do is I'm going to simply copy this line,

108
00:05:11,230 --> 00:05:12,160
OK, copy

109
00:05:12,880 --> 00:05:13,730
paste.

110
00:05:13,980 --> 00:05:17,660
And now let us try using a different password. So

111
00:05:17,899 --> 00:05:21,220
I'm just gonna go capital AB C 123

112
00:05:21,829 --> 00:05:25,299
and OK, that's it. So let me go ahead right now,

113
00:05:25,739 --> 00:05:26,940
we run the program again

114
00:05:27,049 --> 00:05:31,459
and now you can see that it is false because our method

115
00:05:31,470 --> 00:05:37,920
has checked AB C 123 is not equal to saltfish 123.

116
00:05:37,929 --> 00:05:38,459
So

117
00:05:38,640 --> 00:05:42,049
this is how we, we can encapsulate

118
00:05:42,209 --> 00:05:43,200
data

119
00:05:43,410 --> 00:05:46,119
using the object oriented programming.

120
00:05:46,209 --> 00:05:50,739
All we really have to do to indicate that a particular attribute is private,

121
00:05:50,750 --> 00:05:51,850
that we want to encapsulate

122
00:05:52,049 --> 00:05:54,540
is to have the double underscores

123
00:05:54,709 --> 00:05:58,160
in front of the actual attribute name. So

124
00:05:58,470 --> 00:06:01,559
that's it. Thank you for watching the video. I will see you in the next class.

