DEV Community

Cover image for Creating a unique password generator using Ruby
Sean
Sean

Posted on

Creating a unique password generator using Ruby

Alright

So how do we go about creating a password generator? It's actually quite simple. I'm going to demonstrate multiple ways that we can do this using Ruby.

Method #1

alpha =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz?/\\ |}{[]!@# $%^&*()1234567890_-+=<>,.`~'':;".split(//)
#sorry about the super long line!
passwrd = ""
passwrdlen =  gets.chomp.to_i
while passwrdlen > 0
  passwrd += alpha.sample
  passwrdlen -= 1
end
puts passwrd
Enter fullscreen mode Exit fullscreen mode

Uhh....

Okay let's break this down.

We defined alpha as a list containing string characters(metachar, nums, etc.). Then we define passwrd as an empty string. We get the users input on the passwrd length and assign it to passwrdlen. Inside our while loop we use the sample method in ruby to add a random character to passwrd and subtract 1 from passwrdlen. Finally, we loop this process until passwrdlen is equal to 0, then we print passwrd to the console.

Method #2

Now we can expand on the previous method to create a better version. So here it is:

alpha = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz?/\\ |}{[]!@# $%^&*()1234567890_-+=<>,.`~'':;".split(//)
passwrd = ""
puts "What word keyword/theme do you want the password to have?:"
theme = gets.chomp
puts "How long do you want the passwrd to be?(Must be longer than the theme.):"
passwrdlen = gets.chomp.to_i
if passwrdlen > theme.length
  passwrdlen -= theme.length
  passwrd += theme
  while passwrdlen > 0
    passwrd += alpha.sample
    passwrdlen -= 1
  end
  puts passwrd
else
  puts "Can't use the theme since password length is shorter than the theme."
end
Enter fullscreen mode Exit fullscreen mode

Hmm... how does this work again?

Again, lets break it down


There are a few minor but wonderful changes. Now the program asks for a word that you'd like in you password. If we're being honest no one can remember a password that is truly random. Then the program does it's usual thing, except there's an if/else statement that determines whether you followed the rule stated above, 'Must be longer than the theme', if not then you don't get a password, and ruby puts 'Can't use the theme since password length is shorter than the theme'. If you did then you'll get your password with the word you chose and random characters behind it.

This is great an all but the code is super long an messy, for a task that's so simple. Can it get shorter?

Yes it can! Aye, Aye Captain!!! 🏴‍☠️

Here's a shorter version of the same code:

alpha, passwrd = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz?/\\ |}{[]!@# $%^&*()1234567890_-+=<>,.`~'':;".split(//), ""
puts "What word keyword/theme do you want the password to have?:"
theme = gets.chomp
puts "How long do you want the passwrd to be?(Must be longer than the theme.):"
passwrdlen = gets.chomp.to_i
passwrdlen > theme.length ? (passwrdlen -= theme.length; passwrd += theme; passwrdlen.times {passwrd += alpha.sample}; puts passwrd) : (puts "Can't use the theme since password length is shorter than the theme.")
Enter fullscreen mode Exit fullscreen mode

The logic is mostly the same, I used the times method to make looping the process faster, and a ternary to make the boolean logic remain.

Write how you would have done it in the comments, in a different language or in Ruby as well.

Discussion (1)

Collapse
codyalvarez profile image
codyalvarez • Edited

SecureRandom but i didnt build it with the functionality to choose a theme. Justs 3 choices : numbers, alphanumeric, and hex - numbers and letters.

Im deff going back and refactoring though so i appreciate this ! Wish i started programming in high school. -__-