New to Ruby, I write a random password generator with a "qwazerty-sensitive" mode.
The program shall ask a desired password's length and whether the password shall include or not characters common to azerty and qwerty keyboards (let's call this the "qwazerty type"). Once the password's parameters set, a password of the desired length and type is generated by pseudo-randomly picking among the ASCII table's printable characters.
At this point, I have to fix the qwazerty mode to get it up and running. The code's as follows :
#!/usr/bin/env ruby
character_set = [ "!", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~" ] # ASCII order, no 032, """, "\", "#".
bad_character_set = [ "!", "$", "%", "&", "'", "(", ")", "*", ",", "-", ".", "/", ":", ";", "?", "@", "A", "M", "Q", "W", "Z", "[", "]", "^", "`", "a", "m", "q", "w", "z", "{", "|", "}", "~" ]
puts """ ,----, ,----, ,---, ,-, ,----,,----,
/ // / / // /,-,-,-, / ,-' / / / // // // /
/ /| |,-, / ,--'/ / / /-, / ',-,-, / /-, / // // // /
'-' '-''-''-' '-----''-''----''-' '-''-''----''----'
R A N D O M P A S S W O R D G E N E R A T O R 1 . 0 0"""
puts "\nEnter the password's length (in arabic numbers)."
password_length = gets.to_i
puts "\nShall the password be qwazerty sensitive (\"yes\" or \"no\") ?"
password_type = gets #.match("/(|^)yes(|$)/i")
if password_type =~ /(|^)yes(|$)/i # == true
character_set - bad_character_set
# character_set.delete( "!", "$", "%", "&", "'", "(", ")", "*", ",", "-", ".", "/", ":", ";", "?", "@", "A", "M", "Q", "W", "Z", "[", "]", "^", "`", "a", "m", "q", "w", "z", "{", "|", "}", "~" )
# character_set.delete_at( 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13, 24, 25, 29, 30, 31, 43, 47, 53, 56, 57, 58, 59, 61, 62, 74, 78, 84, 87, 88, 89, 90, 91 )
else
end
password = Array.new
while password_length > 0 do
password << character_set.sample
password_length -= 1
end
puts "\n" + password.map(&:inspect).join('').to_s.gsub('"', '')
Most commented lines are remnants of previous failures I left if it could ever help. As I tried many different approaches, I cannot remember everything I did.
Post-scriptum : Let us not bother about wrong inputs (such as characters instead of numbers for the password's length) for now.
Aucun commentaire:
Enregistrer un commentaire