Skip to content

Instantly share code, notes, and snippets.

@mach3
Created October 23, 2010 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach3/641943 to your computer and use it in GitHub Desktop.
Save mach3/641943 to your computer and use it in GitHub Desktop.
[Ruby Excersize] Password Generator
#!/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