Coding Challenge Generate Random Passwords

Video

Transcript (Unedited)

what's going on good people this video i'm going to continue building out a coding challenge that i did with the password generator some time ago what i want to be able to do is to have this password generator be able to spit out multiple passwords at one time so basically you call the program with define some arguments and then it will give you know an output without you having to manually interact with the program the reason i want to do this is i want to be able to uh extend the functionality of the existing program but then also be able to do give it everything it needs in a single command and then it gives me everything back and then from there i could potentially use this in let's say a automation script to create users or use it in a script for like web driver for creating accounts or something of that nature and then also when it comes to the length of the password in the original coding challenge we had to specify the length of numbers letters and symbols this video i want you to try to get that so that if you don't provide or if you provide a argument it will just automatically come up with those numbers within a given range and then continue with the rest of the process that being said let's get started [Music] all right so this code will be available on github so if you want to have a copy of it by all means you can go to my github in the coding challenge repository this will be the password generator project and what i want to do so this code has not been changed it may have been modified a little bit i think i did add a function here to process the inputs and what i want to do is actually change that so that the if a value is not provided or if an argument is not provided for it to randomly figure out a number for each of these types then it prompts the user for the value that should be used so what i'm going to do i'm going to go ahead and copy um let's see want to copy these remaining lines here and not do that all right and let's go ahead and do format to rearrange that so i want to do a if um and i guess we can specify the arguments here and what i like to do is when i do my arguments i have a function that handles the arguments and based on those functions it based on what arguments are set it defines some variables in here so i'm going to create a pra private not practice private static [Music] um we'll call it void for now and then we'll call it process arguments and we'll do a string of args and this is literally being called after the password generator so we want to do process arguments and we are literally passing in what is being passed into the main so this goes into our process arguments function from this uh we'll probably set some class level variables because um well let me write this function out here because i probably don't actually even need a i'll probably put it into a function after i get done with everything but to initially write all of this out is not necessary so what i want to do is loop over every argument that is being provided so i'm going to do a for each bar argument in args and then for uh so what we want to do is if argument is equal to and we'll do a double dash um now you can do a single dash or double dash i'm using a linux computer and normally when you're providing if you're doing shorthand you do a single dash if you're doing the long hand version of an argument you do a double dash and to further demonstrate that example so let's do a grep and dash h which is shorthand and so it gives you that but as you see here even in this shorthand help it says try grep with the double dash help for more information and so if i do double dash help it gives you a whole lot more output so um that's kind of the way you could do it so i could do it with the shorthand i could do it with the longer hand or i could do both um another example of this since we are coding in net core so dot net core dash h gives you the help information here and as you see here it has two options so it has the dash h for the shorthand and then the double dash help for the long hand same thing for diagnosis you got the dash d and double dash diagnosis so like i say i'm gonna i'm gonna do it with the double dash i can probably go back and code it later with the single dash as an option as well so this argument we want to do uh we'll call it random length and i guess we can go ahead and do the single option as well so if argument random length or argument is equal to dash r we'll just call it that for short all right then we want to do um i have a random down here so what i'm gonna do is actually move this up in the code and i'm going to put it here and that way i can leverage it within my for each so we want to do want to copy [Music] let's see so i need these four items as well so what i'm gonna do is delete these and copy and move these up here and so i will so we'll initialize them to zero um let's see formats there we go all right so we'll initialize them to zero and then in here if we got random length what i would do is do number of letters is equal to random and we want to set the min range um i'm going to define this as int and the mid range we'll set it to 3 [Music] and then on the same line do max range and we'll set it to six all right so that being said with mem range and max range um what i'm going to do with those values is set the range for the random so the minimum number and i may need to change this up a little bit for well i'll take that back out so let's do this so we want our password to be a minimum number of letters but we also want to have a maximum number of letters numbers and symbols usually not just letters so what i want to do is do random.next and um ideally we can use symbols in there but we want to have a minimum length so to get uh ideally from my experience systems have a minimum length of 8 characters as a password and a maximum length of like 20 or something like that nature so i want to do a combination that would be on the low end eight and on the high end let's say we'll actually go with 16 because 20 seems while that would be a more secure password 20 is kind of pushing the limit of a lot of systems so to get uh so we'll do next for so we'll do a min value and um we'll do eight on this one plus letters so next we'll do symbols random dot next and we'll do two and uh four and then of numbers random dot next is two and four see this right so four and four is i can highlight it correctly so 4 and 4 is 8 and 8 to 8 is 16 so we have our max of let's do this again all right so here we have 4 four and four is eight and then you have eight onto that so it max length of 16 on the minimum you have two two which is four and another four which is eight so it will pick a password between 8 and 16 characters if we have the random length argument specified all right the next thing we want to do is to specify the number of passwords and so in order to do that because right now the way it's currently set it will only generate one password per run so what i want to do is to expand this and so we'll say if argument is equal to um let's see really let me change the code up a little bit so what i think i want to do now to make this a little bit easier i'm going to do a switch statement instead of if condition you can do this with the if condition but i think once you get to a certain number of if statements it becomes more efficient to use a switch statement instead of an if statement so that's what i'm going to go ahead and switch so i'm going to take the same functionality up here and just move it and put it within a case so we'll do random length and then we'll do a another case statement with uh dash r and then we will paste our numbers and so forth here and then we'll do a break and then as always you'll want to specify a default condition even if there is nothing to do with in no action to actually do in that default condition i would always make it a habit of specifying it so then the next case will be a um we'll call it dash in and above that i'm going to do case and put a little bit more space in here so we'll do uh num uh we'll do count we'll call it count and then we'll do this as dash c that way it makes a little bit more sense and so we will want to do the and i have a process input function [Music] um i don't want to well i want to be able to use this logic here uh to make sure that it is an integer um but i don't want it to prompt so what i may do is for the time being i'm gonna copy this out of here and paste it back and then when i get to a point of refactoring i'll probably uh modify the code accordingly so that it does uh a more valid uh it doesn't right have the same code written in it more than once all right so we're not writing an input and we are taking the arguments um for this i actually need the argument plus one so uh i'm gonna have to get rid of this for statement and do it i equals zero i less than args dot length i plus plus and for this you want to do [Music] args i and if when we come down here so we want to do args i plus one and so what the what this will do is so if the current argument is da double dash count or dash c then we want to get the integer value specified with the the integer value specified after these arguments so that's where you get the arguments which is the current argument plus one which would be that specified number and then we want to convert that into an integer and then we want this to be um we want this to be a variable so that needs to be [Music] password gen count i guess or uh pass yeah password you can count is equal to zero and then we'll set password gen counts or set the out the number specified to password you can count assuming that it does not throw an exception if it does throw an exception [Music] um you don't need a break statement there if it does throw an exception you want the program to exit [Music] so see uh not sure how to do that here um so what i'm gonna do i know i want the program to exit if they throw an invalid argument so what i'm gonna do is actually just take out the try block for the time being until i have a little bit of a moment to go back and research that all right so just to recap what we've done thus far so we have a for loop and we can get rid of that statement i have a for loop that will loop through all of the arguments that have been provided and from here we'll have the each of those values for if you provide the random length argument we'll do this here if you provide the count it will do this here and we need to do one more um option here and so that case will be if dash h and then we'll also do another one here where the case is help because we want to provide some help output and so i want to call a function print help and do a break and then here we'll do control dots and we'll create a method called print help and if we go to that definition so this will be a combination of or well it will consist of not a combination of console write lines that output the usage so for example how we have here this long output for the net that's h similar in nature for that so what i'm gonna do is actually kind of mimic this so we'll go with on this first line usage and then we'll say um password [Music] generator t or spelling is critical and then um arguments that's on that line then another console right line and you could do this console.writeline as one long string uh to make it easier to manage and come to think of it i probably will just do that so let's take this back so we'll do a string of help outputs and we'll do that and then um let's see so we have that one there do that one plus that's not right um wait a minute yeah that's okay so i am missing a um a new line i think is that correct let's see console. no okay i'm getting my technologies mixed up here all right so to do that so slash r slash n and that's a difficult thing to type so what i'm gonna do is because that character turned it's a difficult thing to type i'm going to type a new line and put that there and then so that new line and if i do a semicolon here that error should oh i'm missing that at the end that error should go away there we go all right so we have that and then plus new line intellisense helps makes things a little bit easier all right so the next thing in this part so they have a description of what the application is so i'm going to call it generate a password with the new line rocket type there we go all right and then we'll do another blank new line plus new line all right the next one is runtime option so we have to provide our arguments so we have a um what do we have we have the uh dash h or whoops need to put it in quotation marks so dash h or that double dash help and provides um help out well we'll say displays this screen um i've seen this done before i know it's kind of a rudimentary way of what the help is but essentially that is what the help is is for it shows you the current screen all right the next one will be the dash r and i think that's what we called it dash r yeah r with random length and c with count all right so random length display uh generates passwords uh put that in parentheses of random length and then the last one is dash c with a count um generates one two three uh specified number of passwords there we go all right so after we do all that then we do help output and remove that last one now we could do uh formatting of this these here so that they tab up um kind of like how this is tabbed up but that's a little bit more advanced so i'm not going to get into that with this particular video um but what you could do it's a cheap uh like not necessarily cheap way but a quicker way of doing it is you space out everything so that it lines up here and then when you run that it will line up correctly all right so let's give this program a go and um actually need to do net run with i believe i can pass the arguments here um no okay so i need to let's do dot net build and once it builds all right so do clear and then cd bin debug net core app 3.1 and then do dot slash password generator double dash help and there we go so we have the help information here um now what should not happen it should not prompt you for that information there so we're going to have to fix that particular part um so where are we so we are here with the help um what i'm going to do is to actually move the remaining parts of the current main uh the current code in main and we will move this highlight the code use control dot and call uh extract method and then we'll say um password let's see um generate password that's a very good not well i mean that is what it's doing but probably not the best usage of that um all right so we'll go with uh generate password all right there we go so with this if um does not need to return that value all right so we'll also need to update this here and what's this needs to do is now so if we're generating the password then um this function needs to be called here as well as no take that back um so if we do a break uh okay this is an interesting setup that i got myself into um all right so outside of the for loop if the so it needs to go outside of the for loop because we want to go make sure that we go through all of the arguments and um if we do go through all the arguments and let's see if we probably need to set another value here and if help is so we'll do a boolean and call do help and set it to false and if come down here and if we print the help then set do help equal to true [Music] and if we did the help so if do help is equal to false then we run the generate password if it's equal to true we want to skip over that all right and so that will i think this will address that issue um as for these if let's see so we need to have so the random numbers at the random numbers so if all the random numbers are equal to zero then yeah so the random number is equal to zero we want to skip over the processing of the input so i'm just going to add if um really you don't have to check all of them because if one is equal to zero is not equal uh no if it's equal to zero then we want to prompt for all of the values because by default these uh variables will be zero all right and then i did it right yes all right so oh so that's the output variable i'm not sure why it specified it to be a oh i know why because we're passing it in but it doesn't need to be an out variable so let's change this these outs to just be variables passed in because what is happening is the it because i use the extract method thing it assumed that these would be out variables and would be reused in the main function but in this particular scenario that's not true all right so we're only checking the number of letters so um because we're only checking the num of letters uh yeah that should be sufficient all right so let's go back so we're here and we want to if we're just performing the help test then we want to skip over that um i know some people are you're probably watching this and be like why is he having set that equal to false well you could also do this because i know some people prefer to do this with their code and it's essentially the same thing but my personal preference is when you're doing these type of statements um it's very easy in some programming languages this one net core not necessarily being one of them but it's very easy to see what i have highlighted here as a single variable instead of an exclamation point with a variable next to it so i try to refrain from doing that and i prefer to write my when i have these type of conditions i prefer to write it equal to false to make it clearly evident that you are looking for a false condition here instead of this uh exclamation with a variable name which is basically a not statement so if do help is not equal to true which is basically saying if do help is equal to false um so you know i try to avoid what is known as uh i think it's called like an inverse condition um that's one of the if i recall correctly is mentioned in rob robert martin's uh clean code book about using inverse conditional statements because the logic behind it is kind of confusing like you can see it and process it when you're actually trying to process your you know think your way through it uh it can be rather confusing to uh process and try to get correct so now that i've done with that spill all right so everything else should be working as it should so if you come back here and do uh i don't know if i can do net build from this directory and i can't all right so we'll go back to the password generator and do net build and i'm going to do is open a new tab in my ssh session and then we'll go to that build directory so dotnet bin all the way down dot slash password generator double slash help and there we go and then if i do a uh dash h it should give me the same output and it does so we know that the help screen is working the way it should be [Music] um yeah there we go all right so the next part is to [Music] we're going to double check your logic right quick and then try to run this and see what happens when we run it um so just to make sure so we have our number of letters all being set so we have here the number of letters is equal to zero but because we're giving it random values if the argument is passed this should never equal zero um it well all of these should never equal zero but in specific specifically we're checking the number of letters because that's the one that we're just only checking um you could write this if condition to check each one of these values to make sure that they're not equal to zero but because of what we have here if the number of letters is not equal to zero it is very unlikely unless something you know uh memory exception or something else occurs that these other two variables would be zero and even if then that something like a memory exception would occur it would throw that exception and probably wouldn't even execute these last two statements um when generating those random values so the rest of this code wouldn't even run all right that being said um the other thing we need to do is to have uh so let's set password drink out to one because not a think about it the you want this to run at least one time um and so if i do this here so a for statement and i want to ant equal zero while i mean excuse me not it um let's call this so we got password gen count uh there let's see we'll stick with i i don't think i have an i in this i do have an i in this so i can't use i well nope i'm sorry so this i is for that loop so i can do i here again um so if i equals zero i less than uh password gen counts and i plus plus and then we want to do the generate password and do format and also do trim real quick all right and so just to recap so when if a user passes in the argument of double dash count or dash c we wanted to convert this to a n32 and what i'm gonna do here is so i'm gonna re-enable this try catch and we'll say invalid input and what will happen here is we'll default the password gen count to one if an exception occurs and i'm gonna do the same thing here um could i have put this statement here in a finally block yes um am i going to do that no because then i'll have to have a condition that will check to see if the password jan is equal to one or greater than one and that's a little bit extra code so it's easier just to drop it and set this value here that if you have an exception of if you throw an exception then just default it to one and that ensures that even if the value that you're typing in for the count is mistyped or uh if if it's not an integer or if it is a it's not entered like if you type the argument but don't type the secondary argument with it that it is caught um and i guess the other thing with this is to also catch i guess a general exception with this particular value because i'm not um i think it's argument null uh or argument exception i'm not sure which one it is so we're gonna go to an argument exception for this and when we test it if it doesn't work the way you would think it would work then [Music] we'll just handle it accordingly and still set it to one that way it will be it won't cause any other issues um and we'll say error occurred because most users don't understand exceptions but they do understand error um so i'm gonna type that there error and then do uh open brace zero close brace with ex dot message as the value to replace the zero placeholder here and we're still going to set that to the password account to one that way we still get a password even if we throw an exception um all right this looks like it is ready to go uh the rest of the logic for actually generating the password is not changed um we just added other things around it to process or you know address what needs to be done and that being said let's go back to the console and we'll do another net build on this console tab all right bill succeeded so let's clear the screen and i'm going to do dot slash password generator and this time i'm going to pass in uh let's see we're going to do a dash r and so it should just give me there we go so it works so we have it generating a random password and i can do this as many times as i want as you see here the length of the password is changing uh each time this these two times they got the same length which is still acceptable um this one is a little bit longer if you kind of draw a line straight down you'll see that it doesn't quite hit and this one is clearly shorter uh i think this one is actually this one is the minimum length of eight characters as you see one two three four and then five six seven eight all right so the next thing is to try out the count and so we'll do dash r c and we'll give it a count of five so this time we should get five passwords there we go and so we have our five passwords and they're all the same length um and i think that has to do with the random generator uh because the random function when you initialize it it i if i'm not mistaken it uses the current time to get a random value um so that is probably playing a part in the length of the password um well i'll take that back no because we're actually setting so we're setting the random link in the beginning and because it's set in a random length in the beginning that means when it does that count then it's going to do that each time okay so what may needs what may uh let's see so let's change some of the logic here so that we get a different password length each time that we do multiple passwords uh so what i want to do is actually to move this um so we're going to move this to a method uh control dots extract method and we'll do we'll leave that there and we'll rename this new method to get random links l-e-n-g-t-h-s there we go all right and so if we go to the definition of that it will set the it will get the random lengths and then return those back to their respective values but that function does not need to go here it needs to actually go down here in our four and so we want to do if random lengths is equal not the function call come on l-e-n-g-t-h-s is equal to well we'll just leave it there so it is understood that if that is true then we want to do this function call and set that here and then we need to define this as a [Music] need um generate a variable and we'll call it a not a property it's not a parameter or is it a no it's not a parameter so it's a it's not a field either okay um not seeing what i wanted to see so we'll do bool random lengths is equal to false and then here we will set random lengths is equal to true and not double equals that's comparison operator all right that should work there and then so when we come down here if get random lengths is yeah so this is true then it will get the random lanes and then we will get random values for our passwords each time so let's go ahead and do a build and if we come back over to our console and do a run with five passwords all right and so now we're getting five passwords with different links as you see here yeah so we're getting five passwords with different links and that's what you should get out of a random ising function [Music] let's see are there any other scenarios that we want to test so we're getting five so let's see what happens if we do the r without let me excuse me do the c without the r all right and so um because we're not doing random passwords but we are doing the count it will prompt five times all right so it gave that password and so it's going to ask again five times how many uh letters do you want in your password and then we'll just keep going all right and so it asked us five times how many letters would we like in our password this does work but i do not see a use case for just this dash c5 option but i'm going to leave it in there just in case um you know i may have a use case for it i don't foresee a use case for it but it is useful to have it now that we've done that let's go back and demonstrate the full values excuse me the full length name of the arguments so we're going to do the same function with the double dash random length as being passed in as an argument and there we go so we can confirm that that works and then if we do just dash r we can also see that that works and then if we do the same thing before with a with the count fully specified you'll see here we get our five passwords with the arguments the full length excuse me the full name of the arguments being specified here too so i believe this works the way that it uh is intended um it's still saying your password is for each of these what could be done is if you are specifying the if an argument count is greater than zero you could possibly trim this out that way you know uh is being you want the program to run in kind of a headless type mode [Music] but i'm not going to do that right now that may be a future enhancement to this uh coding challenge and so you're probably asking well what happens if you don't pass any arguments so let's go ahead and try that option as well and as you see it works as it did in the original coding challenge where it asks you how many of each that you want and then it spits out a password in the end so that will wrap things up for this particular video uh this uh i'll continue to add on to these existing coding challenges that i've done so that you know how to add additional features to your particular application that you're building if this video has been helpful or if you have any questions about anything that you've seen in this video please give me a thumbs up if you like it and then drop your question down below afterwards or your comment down below afterwards as well uh if this is your first time to the channel please be sure to consider subscribing each week a new tech video comes out on tuesday i also have videos that come out on saturday so do subscribe do give it a thumbs up till next time. peace

I enjoy sharing the content that you find here. I ask that you consider purchasing an item on my Amazon Wish List or you can make a donation.

Back To Top