Skip to content

Instantly share code, notes, and snippets.

@funny-falcon
Created February 13, 2012 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funny-falcon/1814635 to your computer and use it in GitHub Desktop.
Save funny-falcon/1814635 to your computer and use it in GitHub Desktop.
Password generator
class String
def randchar
self[(rand * size).to_i]
end
end
Vowels = 'aeiouy'
Consonants = 'bcdfghjklmnpqrstvwxz'
VowelsR, ConsonantsR = [Vowels, Consonants].map{|s| /[#{s}]/}
Chars = [Vowels, Consonants]
def gen_pass(len)
res = ''
i = (rand * 2).to_i
while res.size < len
res << Chars[i].randchar
i = (i+1) % 2
end
res
end
DEFAULT_LENGTH = 12
DEFAULT_NUMBER = 3
require 'optparse'
opts = {}
opt = OptionParser.new
opt.on('-l','--length [LENGTH]',Integer,"length of password (default #{DEFAULT_LENGTH}"){|i|
opts[:length] = i || DEFAULT_LENGTH
}
opt.on('-n','--number [COUNT]',Integer, "number of password (default #{DEFAULT_NUMBER}"){|n|
opts[:number] = n || DEFAULT_NUMBER
}
opt.on('-h','--help') { puts opt; exit}
opt.parse(ARGV)
unless opts[:length]
print "Length of passwords[#{DEFAULT_LENGTH}]: "
s = $stdin.gets
opts[:length] = s.to_i if s.to_i > 0
end
unless opts[:number]
print "Number of passwords[#{DEFAULT_NUMBER}]: "
s = $stdin.gets
opts[:number] = s.to_i if s.to_i > 0
end
opts = {:length=>12, :number=>2}.merge(opts)
puts "Passwords:"
opts[:number].times do
puts gen_pass(opts[:length])
end
@funny-falcon
Copy link
Author

$ ruby genpas.rb -l 13 -n 4
Passwords:
ebamazofifira
lufahavelozak
pikisolocamiz
uwosesonymaje

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment