Created
October 23, 2010 08:26
-
-
Save mach3/641943 to your computer and use it in GitHub Desktop.
[Ruby Excersize] 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
#!/usr/bin/ruby | |
class MkPasswd | |
def initialize | |
@defaultOption = { | |
'length' => 8, | |
'useNumeric' => true, | |
'useUpperCase' => true, | |
'useSymbol' => true | |
} | |
@numeric = '123456789' | |
@alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
@symbol = '#$-=?@[]_' | |
end | |
def generate ( option = {} ) | |
@defaultOption.each { |key,value| | |
if option[key] === nil then option[key] = value end | |
} | |
map = @alphabet + | |
(( option["useNumeric"] ) ? @numeric : "" ) + | |
(( option["useUpperCase"] ) ? @alphabet.upcase : "" ) + | |
(( option["useSymbol"] ) ? @symbol : "" ) | |
map = map.split("") | |
result = "" | |
option["length"].times { | |
result += map[ rand( map.length ) ] | |
} | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment