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
-
.path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.add(pass, comm = nil) ⇒ Object
Add password to list.
-
.finalize ⇒ Object
Save list of passwords as txt file.
-
.generate_list(chars_no, opts = {list: 10}) ⇒ Object
Generate list of passwords, and save as plain txt.
-
.nocopy! ⇒ Object
Disable password(s) copy (affect only executable).
-
.nocopy? ⇒ Boolean
Check if password(s) will be copied to clipboard.
-
.noprint! ⇒ Object
Do not print password(s) to STDOUT (affect only executable).
-
.noprint? ⇒ Boolean
Check if password(s) will be printed to STDOUT.
-
.nosave! ⇒ Object
Do not save password list as txt file.
-
.nosave? ⇒ Boolean
Check if password list will be saved as txt file.
-
.randpass(chars_no = nil) ⇒ String
(also: [])
Generate random password.
-
.reload ⇒ Object
Clear old password list.
-
.storage ⇒ Object
Access password list.
Class Attribute Details
.path ⇒ Object
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
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 |
.finalize ⇒ Object
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.
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
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 @output = false end |
.noprint? ⇒ Boolean
Check if password(s) will be printed to STDOUT
25 26 27 |
# File 'lib/randpass.rb', line 25 def @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.
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
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 |
.reload ⇒ Object
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 |
.storage ⇒ Object
Access password list
61 62 63 |
# File 'lib/randpass/storage.rb', line 61 def storage @temp end |