1 00:00:00,009 --> 00:00:04,590 Welcome back. So now let's add one more security feature 2 00:00:04,960 --> 00:00:08,069 to our program. And in here, 3 00:00:08,220 --> 00:00:12,470 we are going to include hashing. We're going to hash 4 00:00:13,210 --> 00:00:14,739 are passwords 5 00:00:14,899 --> 00:00:17,500 by making use of the sha 6 00:00:17,690 --> 00:00:21,549 256, the secure hash algorithm 7 00:00:21,670 --> 00:00:25,670 with of course a data size of 256 bits. So 8 00:00:25,950 --> 00:00:27,010 to do this, 9 00:00:27,219 --> 00:00:31,790 we are going to have to import another library 10 00:00:32,209 --> 00:00:35,220 and this one is going to be the hashlib. 11 00:00:35,330 --> 00:00:36,430 OK? So 12 00:00:36,830 --> 00:00:41,490 let me just add a node here and also import for hashing 13 00:00:42,590 --> 00:00:44,060 and there we go. All right. 14 00:00:44,409 --> 00:00:45,130 Now 15 00:00:45,330 --> 00:00:51,729 let us add it at the very top in here the function to actually hash the password. So 16 00:00:52,209 --> 00:00:56,599 let me just add a note function to hash passwords. 17 00:00:57,020 --> 00:00:58,169 OK? So 18 00:00:58,849 --> 00:01:03,630 the name of the function I can say define and we can go with hash password. 19 00:01:03,869 --> 00:01:05,290 And now of course, inside the brackets, 20 00:01:05,300 --> 00:01:07,879 we're going to be working with the password parameter 21 00:01:08,139 --> 00:01:09,160 and then I put my column. 22 00:01:09,750 --> 00:01:11,160 So now I'll take a look at this. OK? To 23 00:01:11,410 --> 00:01:13,349 hash the passwords, 24 00:01:13,949 --> 00:01:16,089 you can simply say return, 25 00:01:17,309 --> 00:01:18,559 OK? And now 26 00:01:19,400 --> 00:01:21,470 the name of the function itself 27 00:01:21,690 --> 00:01:23,120 in the hashlib module 28 00:01:23,279 --> 00:01:24,069 is hashlib. 29 00:01:25,500 --> 00:01:27,110 OK? And now dot 30 00:01:27,550 --> 00:01:27,970 Sha 31 00:01:28,269 --> 00:01:30,540 256. 32 00:01:31,370 --> 00:01:33,019 And now in brackets, 33 00:01:33,769 --> 00:01:36,190 what are we hashing is the password? 34 00:01:36,889 --> 00:01:39,709 And now I'm going to use the method called encode, 35 00:01:40,790 --> 00:01:42,900 open up a new pair of brackets, 36 00:01:43,319 --> 00:01:46,449 close the previous brackets. And now dot 37 00:01:46,889 --> 00:01:49,629 And now finally, we can add a hex 38 00:01:49,779 --> 00:01:51,209 digest, 39 00:01:52,500 --> 00:01:56,040 add closing brackets again. And there it is. 40 00:01:56,239 --> 00:01:59,669 So this is a particular line that you can memorize. 41 00:01:59,779 --> 00:02:03,150 This is the function that will actually uh hash 42 00:02:03,160 --> 00:02:07,080 up passwords by making use of the hashlib module. 43 00:02:07,339 --> 00:02:10,419 So all we need to do right now, very simply 44 00:02:10,600 --> 00:02:16,039 will be to integrate it into the actual registration function. So 45 00:02:16,309 --> 00:02:18,279 let us come all the way down here. 46 00:02:18,710 --> 00:02:19,320 Ok. 47 00:02:19,910 --> 00:02:24,880 And at the very end where the password was successful. 48 00:02:25,020 --> 00:02:28,440 Ok. So here where it says print feedback, return 49 00:02:28,729 --> 00:02:33,580 just before we begin to write the password to the users dot Text file, 50 00:02:33,610 --> 00:02:35,110 let us now hash it. 51 00:02:35,119 --> 00:02:37,490 Ok. So after the return statement, 52 00:02:38,029 --> 00:02:38,770 ok, 53 00:02:39,160 --> 00:02:44,240 I am going to add like a small little notes and we can say hash 54 00:02:45,039 --> 00:02:46,339 uh the password. 55 00:02:47,000 --> 00:02:47,539 Ok. 56 00:02:47,679 --> 00:02:48,100 All right. 57 00:02:48,850 --> 00:02:49,350 So 58 00:02:49,470 --> 00:02:50,990 what we want to do right now is very, 59 00:02:51,000 --> 00:02:56,190 very simply we can create a verbal called hashed underscore password. 60 00:02:56,490 --> 00:03:00,350 And then we can pass the actual password that we've just hash, I'll say hash 61 00:03:00,869 --> 00:03:03,869 underscore password and now in brackets 62 00:03:04,199 --> 00:03:06,910 very simply password. 63 00:03:07,259 --> 00:03:08,630 And there it is. 64 00:03:08,910 --> 00:03:11,690 So all we have to do very simply is where 65 00:03:11,699 --> 00:03:14,669 we write in the password instead of the original password, 66 00:03:14,830 --> 00:03:17,660 we'll now have to write the hashed password. So 67 00:03:17,800 --> 00:03:20,750 here where it says file dot write f username 68 00:03:21,080 --> 00:03:23,490 and now in here instead of password, 69 00:03:23,580 --> 00:03:27,300 we're going to change that to the variable hashed 70 00:03:27,639 --> 00:03:30,570 underscore password. 71 00:03:30,919 --> 00:03:35,289 And there it is, we've successfully added hashing. 72 00:03:35,639 --> 00:03:38,979 So let us see if this will actually work. 73 00:03:38,990 --> 00:03:41,350 I'm gonna go ahead right now and run the program. 74 00:03:41,830 --> 00:03:44,619 Ok. And uh let us register. 75 00:03:44,919 --> 00:03:51,369 So I'm gonna add a new name in here. I'm gonna call this one, let's say David, 76 00:03:52,350 --> 00:03:56,289 OK, password. I'm gonna go with a very strong password in here, sword, 77 00:03:57,029 --> 00:03:58,990 fish, 78 00:04:00,110 --> 00:04:01,050 sword, 79 00:04:01,520 --> 00:04:02,410 fish, 80 00:04:02,850 --> 00:04:05,839 and then 234 at and then asteric 81 00:04:06,720 --> 00:04:07,649 symbol. 82 00:04:07,910 --> 00:04:09,380 And there you go. Ok? 83 00:04:09,500 --> 00:04:15,190 So now let's open up the users or text file and see and wonderful. There you go. 84 00:04:15,429 --> 00:04:19,260 So this right here is the hashed version 85 00:04:19,579 --> 00:04:20,329 of 86 00:04:20,579 --> 00:04:21,928 David's password, which is 87 00:04:22,149 --> 00:04:25,809 Salish 234 at and the Asterik symbol. 88 00:04:26,380 --> 00:04:27,480 So wonderful, 89 00:04:27,640 --> 00:04:29,660 we have added hashing 90 00:04:29,869 --> 00:04:33,609 to our program. It's becoming more and more advanced 91 00:04:33,790 --> 00:04:38,880 and now jump in the very next lesson where we're going to add our audit to feature. 92 00:04:38,890 --> 00:04:39,760 I will see you then. 93 00:04:40,119 --> 00:04:42,760 So I just wanted to give you a quick update. 94 00:04:42,769 --> 00:04:46,040 Yes, we've just added the hashing function. 95 00:04:46,459 --> 00:04:51,239 However, when we test it out, you might notice that it doesn't work. 96 00:04:51,679 --> 00:04:57,339 So if I was to go over here right now, and I wanted to register a new user and I said, 97 00:04:58,109 --> 00:05:01,739 let's call the new user Victor, right? 98 00:05:02,380 --> 00:05:02,630 Victor. 99 00:05:02,959 --> 00:05:03,540 And then 100 00:05:03,869 --> 00:05:05,429 I added a password, Victor 101 00:05:05,619 --> 00:05:07,100 123 102 00:05:07,510 --> 00:05:08,799 and then VIC 103 00:05:09,589 --> 00:05:11,029 and then the add symbol 104 00:05:11,220 --> 00:05:12,299 and I press enter, 105 00:05:12,470 --> 00:05:15,700 it does say that user registration was successful. However, 106 00:05:16,100 --> 00:05:18,299 if I try to log in 107 00:05:18,760 --> 00:05:19,570 as Victor 108 00:05:19,799 --> 00:05:20,980 again, 109 00:05:21,459 --> 00:05:23,029 so I have the username 110 00:05:23,429 --> 00:05:25,959 and now if I type in the password 111 00:05:26,450 --> 00:05:27,619 123 112 00:05:28,100 --> 00:05:28,769 VIC 113 00:05:29,130 --> 00:05:29,489 at 114 00:05:30,209 --> 00:05:34,640 you will see right now that it says invalid username or password. 115 00:05:35,040 --> 00:05:40,260 So for some reason, the login function, it's not able to detect 116 00:05:40,450 --> 00:05:43,070 that the username matches the password. 117 00:05:43,420 --> 00:05:47,209 And this is one of the reasons why you really want to test out your code 118 00:05:47,450 --> 00:05:49,880 whenever you've added new functionality. 119 00:05:50,130 --> 00:05:54,230 So I realized that over here online 60 120 00:05:54,690 --> 00:05:58,230 where it says where we have the login function, it says 121 00:05:58,440 --> 00:06:04,470 that if username equal stored username and password equalto password, 122 00:06:04,660 --> 00:06:07,869 then we can allow the user to successfully log in. 123 00:06:08,130 --> 00:06:10,859 But you can see right now that this isn't going 124 00:06:10,869 --> 00:06:14,869 to work because as the code is written right now, 125 00:06:15,279 --> 00:06:16,910 it is comparing 126 00:06:17,609 --> 00:06:21,579 the password to the plain text format and 127 00:06:21,589 --> 00:06:25,109 not the encrypted version or the hash version. 128 00:06:25,119 --> 00:06:25,420 Rather. 129 00:06:25,829 --> 00:06:29,339 That's why we are having the, the, the the issue. 130 00:06:29,609 --> 00:06:33,540 So we're supposed to come in right now and say start username and, 131 00:06:33,809 --> 00:06:36,649 and now we stress the hash 132 00:06:36,920 --> 00:06:39,109 underscore password 133 00:06:39,339 --> 00:06:41,190 and now in brackets 134 00:06:42,269 --> 00:06:43,220 password. 135 00:06:43,869 --> 00:06:46,380 So this right here, we are now telling 136 00:06:46,600 --> 00:06:48,549 the program and telling Python that hey, 137 00:06:48,850 --> 00:06:50,000 compare 138 00:06:50,250 --> 00:06:55,130 the hashed password of the user and not the plain version. 139 00:06:55,420 --> 00:06:59,450 So it should work right now if I was to go back and let us create a 140 00:06:59,869 --> 00:07:02,720 new account for the user, actually, let's uh 141 00:07:03,399 --> 00:07:05,130 stop the program. Ok? 142 00:07:05,279 --> 00:07:07,260 And I'm gonna run it afresh all over again. 143 00:07:07,640 --> 00:07:12,059 So now let me register a new user and call the user mac and then 144 00:07:12,459 --> 00:07:16,019 uh mac 123 at mac 145 00:07:16,209 --> 00:07:17,420 for the password. 146 00:07:17,649 --> 00:07:21,279 And now let's try to log in as Mac again. So I'm gonna say mac 147 00:07:21,660 --> 00:07:25,029 and now mac 123 148 00:07:25,359 --> 00:07:25,950 at 149 00:07:26,209 --> 00:07:27,070 Mac. 150 00:07:27,339 --> 00:07:30,980 And now you can see the login was in fact successful. 151 00:07:31,269 --> 00:07:37,470 So this is again a reminder that whenever you add new functions and new lines of code, 152 00:07:37,679 --> 00:07:39,929 you want to test before 153 00:07:40,739 --> 00:07:43,100 you begin adding more and more 154 00:07:43,250 --> 00:07:46,390 lines of code to your program. So please 155 00:07:46,589 --> 00:07:48,839 do go ahead and update 156 00:07:49,149 --> 00:07:52,660 the line where it says if username, call to username and password, 157 00:07:52,670 --> 00:07:54,950 you want to make sure that it's the hashed 158 00:07:55,089 --> 00:07:59,200 version of the password that we are comparing. Thank you for watching. 159 00:07:59,209 --> 00:08:01,320 And of course, I will see you in the next class.