module RaspbergPassword
RaspbergPassword — port of Raspberg Password Generator 1.1 (VB6) algorithm.
Constants
- VOWELS_LOWER
- VOWELS_UPPER
Public Instance Methods
Source
# File lib/raspberg_password.rb, line 65 def append_random_char!(password, opts, custom:, custom_len:, rand:) loop do code = rand.call(33, 126 + custom_len) next if reject_letter?(code, opts, custom_len: custom_len) if code < 127 password << code.chr else index = code - 126 char = custom[index - 1, 1] password << char if char && !char.empty? end break end end
Source
# File lib/raspberg_password.rb, line 81 def append_vowel!(password, opts, rand:) pool = if opts[:upper] VOWELS_LOWER + VOWELS_UPPER else VOWELS_LOWER end password << pool[rand.call(0, pool.length - 1)] end
Source
# File lib/raspberg_password.rb, line 13 def default_rand(min, max) SecureRandom.random_number(max - min + 1) + min end
Source
# File lib/raspberg_password.rb, line 17 def generate(opts, rand: method(:default_rand)) length = opts[:length] custom = opts[:custom].to_s custom_len = custom.length password = +"" first_letter = opts[:vowl] ? rand.call(1, 2) : 0 (1..length).each do break if password.length >= length unless first_letter == 2 append_random_char!(password, opts, custom: custom, custom_len: custom_len, rand: rand) return password if password.length >= length end first_letter = 0 append_vowel!(password, opts, rand: rand) if opts[:vowl] return password if password.length >= length end password end
Source
# File lib/raspberg_password.rb, line 40 def generate_many(opts, rand: method(:default_rand)) count = opts[:count].to_i count = 1 if count < 1 count = RaspbergPasswordOptions::MAX_COUNT if count > RaspbergPasswordOptions::MAX_COUNT Array.new(count) { generate(opts, rand: rand) } end
Source
# File lib/raspberg_password.rb, line 48 def reject_letter?(code, opts, custom_len:) return true if !opts[:lower] && code > 96 && code < 123 return true if !opts[:upper] && code > 64 && code < 91 return true if !opts[:numbers] && code > 47 && code < 58 unless opts[:extra] return true if code > 32 && code < 48 return true if code > 57 && code < 65 return true if code > 90 && code < 97 return true if code > 122 && code < 127 end return true if !opts[:custom_enabled] && code > 126 false end