Created
February 13, 2012 07:33
-
-
Save funny-falcon/1814635 to your computer and use it in GitHub Desktop.
Password generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
funny-falcon
commented
Feb 13, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment