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.(__FILE__).gsub('/lib/randpass/storage.rb', '')
- VERSION =
gem version
'0.2.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 file.
-
.finalize ⇒ Object
Write list as .txt file.
-
.generate_list(chars_no, opts = {list: 10}) ⇒ Object
Generate list of passwords, and save as plain txt.
- .nocopy! ⇒ Object
- .nocopy? ⇒ Boolean
- .noprint! ⇒ Object
- .noprint? ⇒ Boolean
- .randpass(chars_no = nil) ⇒ String (also: [])
-
.storage ⇒ Object
Access password list.
Instance Method Summary collapse
-
#randpass(chars_no = nil) ⇒ String
Random number of times try to add random special character.
Class Attribute Details
.path ⇒ Object
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
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 |
.finalize ⇒ Object
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.
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
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 @output = false end |
.noprint? ⇒ Boolean
25 26 27 |
# File 'lib/randpass.rb', line 25 def @output == false end |
.randpass(chars_no = nil) ⇒ String Also known as: []
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 |
.storage ⇒ Object
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.
23 24 25 |
# File 'lib/randpass/random.rb', line 23 def randpass(chars_no = nil) Randpass[chars_no] end |