Rubyでパスワード生成

気分転換にパスワード生成をRubyで書いてみました。

class Password
  LCASES = ('a'..'z').to_a
  UCASES = ('A'..'Z').to_a
  NUMBERS = ('0'..'9').to_a
  SYMBOLS = ['#','$','%','&','@','?','*']
  EXLISTS = ['l', '1', 'o', 'O', '0']

  def set_complexity(level=2)
    case level
    when 0
      elements = LCASES
    when 1
      elements = LCASES + UCASES
    when 2
      elements = LCASES + NUMBERS
    when 3
      elements = LCASES + SYMBOLS
    when 4
      elements = LCASES + UCASES + NUMBERS
    when 5
      elements = LCASES + NUMBERS + SYMBOLS
    when 6
      elements = LCASES + UCASES + NUMBERS + SYMBOLS
    end

    return elements
  end


  def generate(length=8, level=2)
    password = String.new
    elements = set_complexity(level)

    while length > 0
      element = elements[rand(elements.length())]
      if EXLISTS.include?(element) == true
        redo
      else
        password += element
      end
      length -= 1
    end
    return password
  end


  def initialize()
    @password = generate()
  end

  def initialize(length=8, level=2)
    @password = generate(length, level)
  end


  def show
    puts @password
  end


  def get
    return @password
  end

end


spam = Password.new(4,6)
spam.show