Module: Passw
- Defined in:
- lib/passw.rb
Class Method Summary collapse
-
.generate(length, options = {}) ⇒ Object
- Generate a password with the sepecified options Params:
length - the length of the password
options -
a hash defining the attributes for the password.
- the length of the password
- Generate a password with the sepecified options Params:
Class Method Details
.generate(length, options = {}) ⇒ Object
Generate a password with the sepecified options Params:
length-
the length of the password
options-
a hash defining the attributes for the password
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/passw.rb', line 6 def self.generate(length, = {}) defaults = { lowercase: true, # Allow lower case characters uppercase: true, # Allow uppercase characters symbols: true, # Allow symbols numbers: true, # Allow numbers duplicates: true # Allow characters to be duplicated (less secure if true) } defaults.merge!() buffer = [] buffer += lowercase if defaults[:lowercase] buffer += uppercase if defaults[:uppercase] buffer += symbols if defaults[:symbols] buffer += numbers if defaults[:numbers] base = [] buffer_length = buffer.length (0...length.to_i).each do |i| if defaults[:duplicates] base << buffer[srand % buffer_length] else loop do candidate = buffer[srand % buffer_length] if !base.include? candidate base << candidate break end # Ensure that this loop does not run forever if duplicates are disallowed # In this case, we're limited to the collective size of buffered characters break if base.length == buffer_length - 1 end end end base.shuffle.join end |