Module: Randpass

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

Overview

Generate random password with SecureRandom#base64 and a few random special characters. Method #randpass is defined as class and instance method, so you can call it or include it.

Constant Summary collapse

ARRAY =

Allowed specials. xxx is just a flag for #add_special_chars

%w[! # * $ % _ @ xxx].freeze
FILE_PATH =
File.expand_path(__FILE__).gsub('/lib/randpass/storage.rb', '')
VERSION =

gem version

'0.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.add(pass, comm = nil) ⇒ Object

Add password to file

Parameters:

  • pass (String)

    Required. Add password to list

  • comm (String) (defaults to: nil)

    Optional. Comment to write with password



18
19
20
21
22
23
# File 'lib/randpass/storage.rb', line 18

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

.finalizeObject

Write list as .txt file



26
27
28
29
# File 'lib/randpass/storage.rb', line 26

def finalize
  File.write "#{@path}/randpass_#{Time.now.to_i}.txt", @temp
  @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)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/randpass/storage.rb', line 39

def generate_list(chars_no, opts = {list: 10})
  storage_init unless initialized?

  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



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

def nocopy!
  @copy = false
end

.nocopy?Boolean

Returns:

  • (Boolean)


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

def nocopy?
  @copy == false
end

.noprint!Object



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

def noprint!
  @output = false
end

.noprint?Boolean

Returns:

  • (Boolean)


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

def noprint?
  @output == false
end

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

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/randpass/random.rb', line 31

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

.storageObject

Access password list



56
57
58
# File 'lib/randpass/storage.rb', line 56

def storage
  @temp
end

Instance Method Details

#randpass(chars_no = nil) ⇒ String

Random number of times try to add random special character. Transform to array, shuffle, and join back to string.

Provide number of characters for password as argument, default value is 25.

Parameters:

  • chars_no (Integer) (defaults to: nil)

    Optional. Number of password characters.

Returns:

  • (String)


23
24
25
# File 'lib/randpass/random.rb', line 23

def randpass(chars_no = nil)
  Randpass[chars_no]
end