Sha256: 20e2aa89f8c2e93f5641df41b4c5bb5a6d3b4c0ca9e565f3394e8c1263fd7337

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

class PasswordAlreadyUsedException < StandardError
  def message
    "This password has already been used"
  end
end

module PasswordUtils
  extend self

  SpecialCharacters      = %w(! @ # $ % ^ & + =)
  LowercaseLetters       = ('a'..'z').to_a
  UppercaseLetters       = ('A'..'Z').to_a
  Numbers                = ('0'..'9').to_a
  PasswordLength         = 8

  def strong?(str)
    return false if str.blank?
    return false unless str =~ Regexp.union(*SpecialCharacters)
    return false unless str =~ Regexp.union(*LowercaseLetters)
    return false unless str =~ Regexp.union(*UppercaseLetters)
    return false unless str =~ Regexp.union(*Numbers)
    true
  end

  def generate_random_password
    password = generate_random(PasswordLength) until strong?(password)
    password
  end

  private
  def generate_random(length)
    "".tap do |str|
      length.times { str << random_character }
    end
  end

  def random_character
    all_possible_characters.sample
  end

  def all_possible_characters
    SpecialCharacters + LowercaseLetters +
    UppercaseLetters + Numbers
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
protected-1.0.4 lib/protected/password_utils.rb
protected-1.0.3 lib/protected/password_utils.rb
protected-1.0.2 lib/protected/password_utils.rb
protected-1.0.1 lib/protected/password_utils.rb
protected-1.0.0 lib/protected/password_utils.rb