class Passcard::Generator
Attributes
options[RW]
Public Class Methods
create_key_file(key, path, options = {})
click to toggle source
# File lib/passcard/generator.rb, line 5 def self.create_key_file(key, path, options = {}) passcard = self.new(key, options) 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
new(secret = nil, options = {})
click to toggle source
# File lib/passcard/generator.rb, line 15 def initialize(secret = nil, options = {}) @secret = secret.to_s.strip.sha512 @options = { "charset" => Passcard::CHARSET }.merge(options) @options.merge!("size" => Passcard::GRID_SIZE, "numeric" => Passcard::NUMERIC_GRID, "alpha" => Passcard::ALPHA_GRID) end
Public Instance Methods
alphanumeric?(r, c)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/passcard/generator.rb, line 23 def run return encrypt_options! if options['grid'] charspace = options['charset'].chars.shuffle numspace = options['charset'].gsub(/[^0-9]+/, '').chars.shuffle alphaspace = options['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 encrypt_options! end
Protected Instance Methods
encrypt_options!()
click to toggle source
# File lib/passcard/generator.rb, line 62 def encrypt_options! Passcard.encrypt!(@secret, @options.to_h) end