lib/pwm.rb in pwm-1.1.0 vs lib/pwm.rb in pwm-1.1.1
- old
+ new
@@ -11,26 +11,31 @@
('2'..'9').to_a) - ['I', 'O', 'l']
end
# Public: Generate a password.
#
- # length - The length of the 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.
+ # Returns the generated password as a String if length >= 8,
+ # or nil is length < 8.
def self.password(length = 16)
- password = ''
- until (password.match(/[A-Z]/) && password.match(/[a-z]/) && password.match(/[0-9]/))
- password = (0..length - 1).inject('') do |pw, n|
- pw + characters[rand(characters.length)]
+ if length < 8
+ nil
+ else
+ password = ''
+ until (password.match(/[A-Z]/) && password.match(/[a-z]/) && password.match(/[0-9]/))
+ password = (0..length - 1).inject('') do |pw, n|
+ pw + characters[rand(characters.length)]
+ end
end
+ password
end
- password
end
end