Module: Pwm

Defined in:
lib/pwm.rb,
lib/pwm/version.rb

Defined Under Namespace

Classes: TooShortException

Constant Summary collapse

VERSION =
'1.2.2'

Class Method Summary collapse

Class Method Details

.charactersObject

Internal: The set of characters from which passwords will be assembled. This could be overridden if you don’t like the default.

Returns the set of characters as an Array.



12
13
14
# File 'lib/pwm.rb', line 12

def self.characters
  (('A'..'Z').to_a  + ('a'..'z').to_a + ('2'..'9').to_a) - %w(I O l)
end

.password(length = 16) ⇒ Object

Public: Generate a password.

length - The length of the password. Minimum is 8. Default is 16.

Examples

Pwm.password
# => 'SPdHeZnn9rut4AUz'

Pwm.password(8)
# => 'oUX4fmqr'

Returns the generated password as a String if length >= 8, or raises exception if length < 8.



30
31
32
33
34
35
36
37
38
39
# File 'lib/pwm.rb', line 30

def self.password(length = 16)
  fail Pwm::TooShortException, "#{length} is too short." if length < 8
  password = ''
  until acceptable?(password)
    password = (0..length - 1).reduce('') do |pw, n|
      pw + characters[rand(characters.length)]
    end
  end
  password
end