Class: PaperAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/paper-auth.rb,
lib/paper-auth/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initializePaperAuth

Returns a new instance of PaperAuth.



7
8
# File 'lib/paper-auth.rb', line 7

def initialize ()
end

Instance Method Details

#create(id, key) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#getObject



91
92
93
94
95
96
97
98
# 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) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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