Module: Randpass

Defined in:
lib/randpass.rb,
lib/randpass/random.rb,
lib/randpass/storage.rb,
lib/randpass/version.rb

Overview

Generate random password with SecureRandom#base64 and a few special characters.

Constant Summary collapse

ARRAY =

Allowed specials (xxx is just a skip-flag)

%w[! # * $ % _ @ xxx].freeze
VERSION =

gem version

'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/randpass/storage.rb', line 9

def path
  @path
end

Class Method Details

.add(pass, comm = nil) ⇒ Object

Add password to list

Parameters:

  • pass (String)

    Required. Password

  • comm (String) (defaults to: nil)

    Optional. Password comment



15
16
17
18
19
# File 'lib/randpass/storage.rb', line 15

def add(pass, comm = nil)
  reload unless @initialized
  line = comm.nil? ? pass : "#{comm}: #{pass}"
  @temp << line
end

.finalizeObject

Save list of passwords as txt file



29
30
31
32
33
34
35
36
# File 'lib/randpass/storage.rb', line 29

def finalize
  unless Randpass.nosave?
    temp = @temp.join "\n"
    @path += '/' unless @path.end_with?('/')
    File.write "#{@path}randpass_#{Time.now.to_i}.txt", temp
  end
  @initialized = false
end

.generate_list(chars_no, opts = {list: 10}) ⇒ Object

Generate list of passwords, and save as plain txt.

Parameters:

  • chars_no (Integer)

    Required. Number of password characters.

  • opts (Hash) (defaults to: {list: 10})

    Optional. Options hash.

Options Hash (opts):

  • comment (String)

    Add comment (name) with password.

  • list (Integer)

    Number of passwords to generate (if no comments)



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/randpass/storage.rb', line 46

def generate_list(chars_no, opts = {list: 10})
  if opts[:comments].nil?
    opts[:list].times do
      add Randpass[chars_no]
    end
  else
    opts[:comments].each do |com|
      add Randpass[chars_no], com
    end
  end

  Randpass.finalize
end

.nocopy!Object

Disable password(s) copy (affect only executable)



20
21
22
# File 'lib/randpass.rb', line 20

def nocopy!
  @copy = false
end

.nocopy?Boolean

Check if password(s) will be copied to clipboard

Returns:

  • (Boolean)


15
16
17
# File 'lib/randpass.rb', line 15

def nocopy?
  @copy == false
end

.noprint!Object

Do not print password(s) to STDOUT (affect only executable)



30
31
32
# File 'lib/randpass.rb', line 30

def noprint!
  @output = false
end

.noprint?Boolean

Check if password(s) will be printed to STDOUT

Returns:

  • (Boolean)


25
26
27
# File 'lib/randpass.rb', line 25

def noprint?
  @output == false
end

.nosave!Object

Do not save password list as txt file.



40
41
42
# File 'lib/randpass.rb', line 40

def nosave!
  @save = false
end

.nosave?Boolean

Check if password list will be saved as txt file.

Returns:

  • (Boolean)


35
36
37
# File 'lib/randpass.rb', line 35

def nosave?
  @save == false
end

.randpass(chars_no = nil) ⇒ String Also known as: []

Generate random password

Parameters:

  • chars_no (Integer) (defaults to: nil)

    Optional. Number of password characters. Default: 25.

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/randpass/random.rb', line 15

def randpass(chars_no = nil)

  chars_no = 25 if [nil, 0, 1].include?(chars_no)

  param = SecureRandom.base64(chars_no)[0...chars_no]

  rand(param.size / 2).times do
    param = add_special_characters(param)
  end

  param.split('').shuffle.join
end

.reloadObject

Clear old password list



22
23
24
25
26
# File 'lib/randpass/storage.rb', line 22

def reload
  @path ||= Dir.pwd
  @temp = Array.new
  @initialized = true
end

.storageObject

Access password list



61
62
63
# File 'lib/randpass/storage.rb', line 61

def storage
  @temp
end