Sha256: 1c9849048e6b5865e2f45f773c295b7f6e1671fb29ccf27b4ab472d9d2e73e70

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

require 'securerandom'
require 'highline/import'

class PasswordGenerator
  
  ALPHA = ('A'..'Z').to_a|('a'..'z').to_a
  ALPHA_NUM = ('A'..'Z').to_a|('a'..'z').to_a|('0'..'9').to_a
  ALPHA_NUM_SYM = (' '..'!').to_a|('#'..'[').to_a|(']'..'~').to_a
  
  # generate a random password. 
  # format of parameter: 
  # rxx - r = type (a ... alpha, n ... alpha + num, s ... alpha + num + sym) and xx = password length 
  # 
  # example: n10 -> GcQfP4OBFh
  # a12 -> DChpRnhpHiha
  # s18 -> JHnXZ<hrcVKorO6mO:
  def generate( params)
    pwd = []
    range = nil
    
    case params[0]
    when 's'
      range = ALPHA_NUM_SYM
    when 'n'
      range = ALPHA_NUM
    when 'a'
      range = ALPHA
    else
      puts 'Invalid parameter: ' + params
      exit 50
    end
    
    # create x random characters
    params[1..params.length].to_i.times do
      l = SecureRandom.random_number(range.length)
      pwd << range[l]
    end
    
    pwd = pwd.join
    # print pwd with single quote escape
    say( "<%= color( 'Generated password: #{pwd.gsub( "'", "\\\\'")}', :yellow) %>")
    pwd
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lockr-0.5.2 lib/lockr/pwdgen.rb
lockr-0.5.1 lib/lockr/pwdgen.rb
lockr-0.5.0 lib/lockr/pwdgen.rb
lockr-0.4.5 lib/lockr/pwdgen.rb
lockr-0.4.4 lib/lockr/pwdgen.rb
lockr-0.4.3 lib/lockr/pwdgen.rb
lockr-0.4.2 lib/lockr/pwdgen.rb
lockr-0.3.0 lib/lockr/pwdgen.rb