-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordGenerator.rb
52 lines (37 loc) · 1.45 KB
/
PasswordGenerator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#list data structure to represent alphabet lowecase and uppercase letters, digits, and special characters
$lowercase = ("a".."z").to_a
$uppercase = ("A".."Z").to_a
$numbers = ("0".."9").to_a
$special = ["!", "#", "$", "%", "&", "'", "(", ")", "*", "+","-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "]", "^", "_", "{", "|", "}", "~"]
$length = 0
$password = []
#minumum of 10 characters to ensure maximum strength
while $length < 10
#prompting user to choose random password generator length
puts "Please enter your desired password length (must be >= 10 characters): "
$length = gets.chomp.to_i
end
#loop over collection charactersets [lowercase, uppercase, numbers, special]
#randomly choose which of these to pick character from
#another random index selection for the character in that set
#do this 1*length times
$i = 0
while $i < $length do
characterSet = rand(0..3)
if characterSet == 0
#lowercase
$password.insert($password.length, $lowercase[rand(0..25)])
elsif characterSet == 1
#upperrcase
$password.insert($password.length, $uppercase[rand(0..25)])
elsif characterSet == 2
#numbers
$password.insert($password.length, $numbers[rand(0..9)])
else
#special
$password.insert($password.length, $special[rand(0..27)])
end
$i +=1
end
#print password in one line for account to use
puts "Your randomly generated password is: "+ $password.join('')