class PaperAuth
Constants
- VERSION
Public Class Methods
new()
click to toggle source
# File lib/paper-auth.rb, line 7 def initialize () end
Public Instance Methods
create(id, key)
click to toggle source
# File lib/paper-auth.rb, line 10 def create(id, key) # Create two hashes, and remove 3 characters from the end hash1 = Digest::SHA2.new(512).hexdigest("#{id}#{key}")[0..-4].upcase hash2 = Digest::SHA2.new(512).hexdigest("#{key}#{id}")[0..-4].upcase # Combine the two hashes to have 250 random characters hash = "#{hash1}#{hash2}" # Divide the 250 characters into a 10x5 array with 5 characters in each table = hash.scan(/#{"(.{5})"*5}/) # Initialize the two parts (top and bottom half) pair1 = Array.new pair2 = Array.new # For each array grid... for i in 0...10 do for j in 0...5 do # Strip generates either 0 or 1. If it is 0, 2 characters will be removed from the text. If it is 1, 3 characters will be removed from the text. strip = rand(0..1) # Generate two random numbers which will be the positition of the number that will be moved. remove1 = rand(0..4) remove2 = rand(0..4) # Make sure that the numbers are not equal. while remove1 == remove2 remove2 = rand(0..4) end # If strip is 0, remove 2 characters. if strip == 0 # Find the current box we are edititing and split it into characters. text = table[i][j].split(//) # Replace two of the numbers with a blank. text[remove1] = "_" text[remove2] = "_" # Initialize and update the value. pair1[i] ||= [] pair1[i][j] = text.join # Find the current box we are edititing and split it into characters. text = table[i][j].split(//) # Save the value of the characters we will be removing. char1 = text[remove1] char2 = text[remove2] # Create a new temporary array which default contains "_". text = Array.new(5, "_") # Update the Array to only hold the two numbers. text[remove1] = char1 text[remove2] = char2 # Initialize and update the value. pair2[i] ||= [] pair2[i][j] = text.join else # Find the current box we are edititing and split it into characters. text = table[i][j].split(//) # Save the value of the characters we will be removing. char1 = text[remove1] char2 = text[remove2] # Create a new temporary array which default contains "_". text = Array.new(5, "_") # Update the Array to only hold the two numbers. text[remove1] = char1 text[remove2] = char2 # Initialize and update the value. pair1[i] ||= [] pair1[i][j] = text.join # Find the current box we are edititing and split it into characters. text = table[i][j].split(//) # Replace two of the numbers with a blank. text[remove1] = "_" text[remove2] = "_" # Initialize and update the value. pair2[i] ||= [] pair2[i][j] = text.join end end end # Load the template. templates = File.expand_path('../templates', File.dirname(__FILE__)) content = File.read(templates + '/paper.html.erb') template = ERB.new(content) # Generate PDF from the template. kit = PDFKit.new(template.result(binding)) kit.stylesheets << templates + '/paper.css' return file = kit.to_file('result.pdf') end
get()
click to toggle source
# File lib/paper-auth.rb, line 91 def get # Generate random number and letter. number = rand(1..10) letter = rand(65..69) letter = letter.chr # Return random coordinate. return "#{number}#{letter}" end
verify(id, key, grid, value)
click to toggle source
# File lib/paper-auth.rb, line 100 def verify(id, key, grid, value) # Create two hashes, and remove 3 characters from the end hash1 = Digest::SHA2.new(512).hexdigest("#{id}#{key}")[0..-4].upcase hash2 = Digest::SHA2.new(512).hexdigest("#{key}#{id}")[0..-4].upcase # Combine the two hashes to have 250 random characters hash = "#{hash1}#{hash2}" # Divide the 250 characters into a 10x5 array with 5 characters in each table = hash.scan(/#{"(.{5})"*5}/) # Split the coordinate of the grid point. grid = grid.split(//) number = grid[0].to_i - 1 letter = grid[1].upcase.ord - 65 # Search the table for the correct answer. answer = table[number][letter].downcase # If the answer equals to the value, then it returns true, otherwise it returns false. if answer == value.downcase return true else return false end end