WEBVTT 00:01.810 --> 00:07.000 So as I said last time, there's still one little security hole, it's actually a pretty big security 00:07.000 --> 00:11.410 hole in our application and I'm going to illustrate exactly what that is, because I described it last 00:11.410 --> 00:12.850 time, but it might be helpful to see it. 00:13.750 --> 00:21.600 So I'm only forgot password page and I'll put an example dot com, send the password and go over to 00:21.620 --> 00:22.270 mail trap. 00:22.570 --> 00:23.230 And there it is. 00:23.770 --> 00:27.000 And let's click on this link and everything seems good. 00:27.040 --> 00:31.990 We've made sure that the URL is tamperproof and that seems perfect. 00:31.990 --> 00:39.280 But the problem is if I view the source for this page and then scroll down to the JavaScript part I 00:39.280 --> 00:45.640 have right here at Nenad example dot com, and that's the value we're using on the backend to look up 00:45.640 --> 00:47.060 the user and reset the password. 00:48.040 --> 00:53.230 So if I was a malicious user, I could simply use one of the various tools at my disposal to change 00:53.230 --> 00:57.480 that email to somebody else's email on the system and change their password. 00:57.490 --> 00:58.330 And that's not good. 00:58.660 --> 01:00.370 So we're going to fix that this time around. 01:00.370 --> 01:03.000 And it might take a couple of lectures, but we'll get started right now. 01:03.850 --> 01:05.530 So let's go back to Visual Studio Code. 01:06.040 --> 01:08.740 And what I'm going to do is create a new package. 01:08.740 --> 01:12.010 So it's available to both my front end and my back end. 01:12.430 --> 01:14.290 And it's going to be an encryption package. 01:14.290 --> 01:19.480 I'll call it encryption, and it's going to have two functions in there, one that encrypts something 01:19.480 --> 01:20.900 and one that decrypt something. 01:21.490 --> 01:25.750 Now, the one thing we're going to have to take into account here is that the encryption algorithms 01:25.750 --> 01:29.830 I'm using require a very specific length for the secret key. 01:30.040 --> 01:32.790 It needs to be exactly thirty two characters long. 01:33.100 --> 01:39.820 And that means I need to go to aiport, go and put my default value for my secret key right here on 01:39.820 --> 01:43.630 line sixty nine to something that's exactly thirty two character long. 01:44.000 --> 01:47.390 I've already made one and copied it to my clipboard so I'll paste it in. 01:48.010 --> 01:49.900 So make sure you have a secret key. 01:49.900 --> 01:53.410 That is exactly thirty two characters long just like this one. 01:53.740 --> 02:01.330 Put it in API, go and make sure you put exactly the same value in Maine, Dongo in the CMD Web package. 02:01.360 --> 02:07.150 So I'll find that there's my secret key and I'll paste in my new one. 02:07.540 --> 02:14.110 OK, so now my key is reset and of course that means if you have existing links on your in your mail 02:14.110 --> 02:18.700 trap or whatever mail server you're using, those don't work anymore the next time you run this application 02:18.700 --> 02:20.340 because we have a different secret key. 02:21.490 --> 02:27.370 So let's now go over to our internal package, internal folder and create a new package. 02:27.370 --> 02:32.970 And I'm going to call mine encryption and see your wife again. 02:34.780 --> 02:39.790 And inside of that will create a new file named Encryption Go. 02:41.380 --> 02:44.440 And of course, it has the package encryption 02:46.960 --> 02:52.000 and the very first thing I'll do in here is the same sort of thing we did with the last package. 02:52.000 --> 02:53.290 We made a prototype. 02:53.500 --> 02:53.890 Come on. 02:53.890 --> 03:01.750 Encryption, which may not be the best name, but I'm going to go with that encryption and it's a stroke 03:01.930 --> 03:07.330 and it has one member, which I'm going to call key, and that is a slice of Vitz, OK? 03:08.350 --> 03:12.550 And of course, that's how we'll initiate or instantiate this package when we need to use it. 03:12.700 --> 03:19.600 That key will be our secret key that I just put in April ago and may not go now. 03:19.600 --> 03:20.470 I'll have two functions. 03:20.830 --> 03:26.800 One that has the receiver E of type A pointer to encryption and I'll call that encrypt. 03:27.190 --> 03:32.680 And it's just going to take one value the text to encrypt, which is a strength and it will return a 03:32.680 --> 03:34.690 string that potentially there. 03:35.650 --> 03:41.920 But I'll have another one func e pointer to encryption and it's going to be called, not surprisingly, 03:42.100 --> 03:42.670 decrypt. 03:44.410 --> 03:51.580 And again, it takes one thing which I'll call text, I'll call it crypto text because it's been encrypted. 03:53.740 --> 04:01.900 It's a string and it returns a string and potentially an so we'll take care of encrypting things first. 04:02.090 --> 04:10.030 OK, so this function first of all, we need to get that plain text that we received. 04:11.240 --> 04:16.520 That text and I did change that into a slice of bytes and I'll just call that doesn't matter what I 04:16.520 --> 04:17.900 call it, I'll call it plain text. 04:20.100 --> 04:26.210 And it's a sign the value of to a slice of bitts, whatever he received as a call to this function. 04:26.500 --> 04:32.860 OK, and then we'll create a block which we're going to use for encryption. 04:33.040 --> 04:39.580 So Block, that's just the name of a variable, potentially an error or assign the value of from the 04:39.580 --> 04:41.770 A.D.s package crypto. 04:41.990 --> 04:43.480 Yes, that's the one I want. 04:43.870 --> 04:50.320 I'm going to call New Cipher and I'm going to handed the key, our secret key that's going to be our 04:50.320 --> 04:50.740 cipher. 04:50.740 --> 04:52.990 And we're creating something using that cipher. 04:53.160 --> 04:59.530 OK, so if error is not equal to nil, we'll just say return nothing in the air. 05:00.130 --> 05:01.030 So something went wrong. 05:02.990 --> 05:06.260 Next, let's create something called ciphertext. 05:11.790 --> 05:18.390 And that's going to be a sign of the value of make and we're going to get a slice of bite, a slice 05:18.390 --> 05:18.610 of the pie. 05:18.640 --> 05:28.310 It's a block size which is built right into the Earth package, plus the length of our plaintext. 05:32.490 --> 05:36.210 So we took our string here text. 05:36.210 --> 05:38.310 We converted it to a slice of bytes. 05:38.460 --> 05:44.760 And down here we're saying it should be the block size, the block size, plus the length of however 05:44.760 --> 05:47.140 long that's that slice of bytes is. 05:47.370 --> 05:49.850 And again, we need to do something with that. 05:49.860 --> 05:53.070 So we've got our ciphertext and now we're going to create something. 05:53.070 --> 05:54.640 I'm going to be OK. 05:55.610 --> 06:02.060 And it's going to use or it's going to be a sign, the value of ciphertext, why don't we just created 06:03.410 --> 06:05.620 and we're getting a section from that. 06:05.630 --> 06:12.780 So it's the Luxor's and that better spell that right or nothing is going to work. 06:13.440 --> 06:24.440 S dot boxes, which are Finnair if and we're going to ignore the first return value, but we are going 06:24.440 --> 06:34.400 to pay attention to the error and we're calling, I'll read full and we want it from Rande Reader the 06:34.400 --> 06:35.180 Rand package. 06:35.480 --> 06:36.200 Let's try that again. 06:37.010 --> 06:39.950 Rand the reader. 06:42.080 --> 06:50.450 Livy and we checked for an error, error is not equal to nil, then we'll say return nothing to an empty 06:50.450 --> 06:51.500 string and the error. 06:55.880 --> 06:58.040 Now we're going to create a variable called stream. 07:00.200 --> 07:02.650 And that's a sign the value of ciphered 07:06.610 --> 07:15.220 dot, new C of the encrypt or be encrypted. 07:17.210 --> 07:24.410 And we're handing it the two things we just created, Block and IP and now we call from that variable. 07:24.410 --> 07:25.730 We just created stream. 07:26.600 --> 07:28.310 It has methods associated with it. 07:28.460 --> 07:37.010 We want X or Key Stream and we hand it ciphertext and we're looking for a e. 07:37.010 --> 07:45.740 S block size, followed by a column, that section of the slice and plaintext. 07:48.690 --> 07:50.470 And now we have an encrypted thing. 07:50.550 --> 07:56.970 The problem is I need to put this on a webpage and the way I can ensure that it will always be available 07:56.970 --> 07:59.490 to me, no matter what form I need it in. 08:00.030 --> 08:05.580 I know that I can display text on a Web page, and I know that I can't display certain kinds of things, 08:05.580 --> 08:07.320 like a slice of bytes on a Web page. 08:07.590 --> 08:09.230 So we'll just convert it to be sixty four. 08:09.780 --> 08:16.500 So we'll return and recall the base sixty four package, which is built into the standard library and 08:16.500 --> 08:16.890 we'll make it. 08:16.890 --> 08:23.520 You are all encoding and we'll encode it to a string, which means it's safe to use on a webpage and 08:23.520 --> 08:27.780 we'll put in ciphertext and we'll return no error. 08:29.040 --> 08:32.390 So that will encrypt things for us which is really helpful. 08:32.550 --> 08:36.570 But of course once it's encrypted, we also want to be able to decrypt it. 08:36.860 --> 08:39.380 Unfortunately, the logic is pretty much the same. 08:39.420 --> 08:41.110 It's just in reverse. 08:41.940 --> 08:48.450 So in our decrypt function, the very first thing I'll do is create a variable called ciphertext. 08:53.380 --> 08:59.530 And I'll ignore the second return value, and that's just going to be based sixty four, we need to 08:59.530 --> 09:00.270 decode it. 09:00.790 --> 09:09.880 So base sixty four, dot URL encoding dot decode but I want to decode string actually decode string 09:10.180 --> 09:13.840 and I take the value of this path to this function, which is crypto text. 09:16.420 --> 09:21.340 Now there is potentially an error there which I'm ignoring and you probably shouldn't, but I'm going 09:21.340 --> 09:22.100 to for right now. 09:23.230 --> 09:29.540 Now again, we get a block and an error and those come from s dot cipher. 09:29.950 --> 09:30.790 Same as before. 09:30.910 --> 09:34.120 And again, the key, which is our secret key, which is exactly. 09:34.120 --> 09:34.960 Thirty two characters. 09:34.960 --> 09:36.100 Long check for an error. 09:37.060 --> 09:40.960 If error is not equal to nil, then return nothing. 09:40.970 --> 09:41.740 And the error. 09:43.900 --> 09:45.470 And let's give ourselves some room here. 09:45.910 --> 09:49.420 Now we make sure that the length of our separate text makes sense. 09:49.420 --> 09:57.340 Just a sanity check if Lenn of ciphertext is less than a block size. 09:57.490 --> 09:59.190 And this is not something that we can decrypt. 09:59.560 --> 10:01.510 So we'll return an empty string. 10:01.520 --> 10:07.600 And here, once we get past that, once again we'll create a variable which I'm calling I.B., which 10:07.600 --> 10:09.210 is conventional for cryptographers. 10:09.220 --> 10:12.580 For some reason, I've never understood why ciphertext. 10:14.860 --> 10:27.970 And we're going to call in square brackets, Colen a block size and now ciphertext is equal to ciphertext. 10:33.150 --> 10:34.560 Dog block size, 10:37.740 --> 10:44.070 followed by a colon to get the part that we want to work with and we call stream again or a verbal stream 10:44.070 --> 10:48.640 again and again, this is a sign the value of cyper, new CBF. 10:49.460 --> 10:59.820 So for new CBF, this time we want CFB, CFB Decrypts. 11:02.210 --> 11:09.500 And we have that block and then we call the built in functions stream dot. 11:10.970 --> 11:19.720 Export history, and it takes ciphertext and it takes ciphertext, which seems odd, but it takes it 11:19.720 --> 11:20.110 twice. 11:20.310 --> 11:20.630 OK. 11:23.340 --> 11:26.670 And now we return for the sprint F. 11:29.230 --> 11:39.820 And I'll put in percentages and ciphertext with nowhere, and of course, I need to report the full 11:39.820 --> 11:42.400 package that empty. 11:45.190 --> 11:47.780 I'll just import Emmanuela doesn't seem to want to do it for me. 11:49.620 --> 11:50.140 Right here. 11:52.710 --> 11:53.230 OK. 11:53.670 --> 11:54.490 No errors. 11:54.660 --> 12:00.420 So here's our utility functions, which are now available from both our front end and our back end, 12:00.420 --> 12:05.570 which means we can encoded on the front end, which we have to when we display that password reset page. 12:06.090 --> 12:11.550 And because we have the same secret key on our back end, we should be able to decode it as well. 12:12.000 --> 12:17.460 So we'll take care of implementing this on our password reset functionality in the next lecture.