Class: Passcard::Generator
- Inherits:
-
Object
- Object
- Passcard::Generator
- Defined in:
- lib/passcard/generator.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #alphanumeric?(r, c) ⇒ Boolean
- #get_character(r, c, alphaspace, numspace, charspace) ⇒ Object
-
#initialize(secret = nil, options = {}) ⇒ Generator
constructor
A new instance of Generator.
- #numeric?(r, c) ⇒ Boolean
- #run ⇒ Object
Constructor Details
#initialize(secret = nil, options = {}) ⇒ Generator
Returns a new instance of Generator.
15 16 17 18 19 20 21 |
# File 'lib/passcard/generator.rb', line 15 def initialize(secret = nil, = {}) @secret = secret.to_s.strip.sha512 @options = { "charset" => Passcard::CHARSET }.merge() @options.merge!("size" => Passcard::GRID_SIZE, "numeric" => Passcard::NUMERIC_GRID, "alpha" => Passcard::ALPHA_GRID) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/passcard/generator.rb', line 3 def @options end |
Class Method Details
.create_key_file(key, path, options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/passcard/generator.rb', line 5 def self.create_key_file(key, path, = {}) passcard = self.new(key, ) FileUtils.mkdir_p(File.dirname(path)) File.open(path, "w") do |f| f.print "-" * 20 + " BEGIN PASSCARD KEY " + "-" * 23 + "\n" f.print passcard.run f.print "-" * 20 + " END PASSCARD KEY " + "-" * 25 end end |
Instance Method Details
#alphanumeric?(r, c) ⇒ Boolean
54 55 56 57 58 |
# File 'lib/passcard/generator.rb', line 54 def alphanumeric?(r, c) return false if r < @options["size"][0] - @options["alpha"][0] return false if c < @options["size"][1] - @options["alpha"][1] return true end |
#get_character(r, c, alphaspace, numspace, charspace) ⇒ Object
42 43 44 45 46 |
# File 'lib/passcard/generator.rb', line 42 def get_character(r, c, alphaspace, numspace, charspace) space = alphanumeric?(r,c) ? alphaspace : charspace space = numeric?(r,c) ? numspace : space space.shuffle[(r + c) % space.length] end |
#numeric?(r, c) ⇒ Boolean
48 49 50 51 52 |
# File 'lib/passcard/generator.rb', line 48 def numeric?(r, c) return false if r >= @options["numeric"][0] return false if c >= @options["numeric"][1] return true end |
#run ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/passcard/generator.rb', line 23 def run return if ['grid'] charspace = ['charset'].chars.shuffle numspace = ['charset'].gsub(/[^0-9]+/, '').chars.shuffle alphaspace = ['charset'].gsub(/[^a-z0-9]+/i, '').chars.shuffle numspace = (0..9).to_a.shuffle if numspace.empty? alphaspace = (('a'..'z').to_a+('A'..'Z').to_a).shuffle if alphaspace.empty? @options['grid'] = @options['size'][0].times.map do |r| @options['size'][1].times.map do |c| get_character(r, c, alphaspace, numspace, charspace) end end.join end |