Clean up Verification lib

This commit is contained in:
Prommah 2015-11-03 08:41:25 +00:00
parent 429939cf6a
commit 2c76afe481

101
lib/verification.rb Normal file → Executable file
View file

@ -1,63 +1,60 @@
module Verification module Verification
def Verification.verify input def self.verify(input)
md5 = Digest::MD5.hexdigest("9WvcZ9hX" + input + "KF7L4luQ").upcase.split(//) md5 = Digest::MD5.hexdigest("9WvcZ9hX" + input + "KF7L4luQ").upcase.split(//)
chars = ["A", "B", "C", "D", "E", "F"] chars = %w[A B C D E F]
nums = [] nums = []
lastPos = md5[31].to_i last_pos = md5[31].to_i
result = "" result = ""
for i in 0..9 (0..9).each do |i|
pos = md5[i].to_i pos = md5[i].to_i
if pos == 0 if pos == 0
pos = lastPos ** (i % 4) pos = last_pos**(i % 4)
elsif (pos % 4) == 0 elsif (pos % 4) == 0
pos = pos * lastPos + i pos = pos * last_pos + i
elsif (pos % 3) == 0 elsif (pos % 3) == 0
pos = pos ** (i % 4) pos **= (i % 4)
elsif (pos % 2) == 0 elsif pos.even?
pos = pos * i + pos pos = pos * i + pos
end end
pos = (pos > 31) ? (pos % 32) : pos pos = (pos > 31) ? (pos % 32) : pos
curChar = md5[31 - pos] cur_char = md5[31 - pos]
curNum = curChar.to_i cur_num = cur_char.to_i
if nums.include? curNum if nums.include? cur_num
if curNum == 0 if cur_num == 0
curChar = chars[pos % 6] cur_char = chars[pos % 6]
else else
curChar = (pos % 10).to_s cur_char = (pos % 10).to_s
end end
curNum = curChar.to_i cur_num = cur_char.to_i
end end
nums << curNum nums << cur_num
result << curChar result << cur_char
lastPos = pos last_pos = pos
end end
return result result
end end
def Verification.uncrap str def self.uncrap(str)
str.to_s.gsub(/[^A-Za-z0-9_\-]/, "") str.to_s.gsub(/[^A-Za-z0-9_\-]/, "")
end end
def Verification.random_string len def self.random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
str = "" str = ""
1.upto(len) do |i| 1.upto(len) do
str << chars[rand(chars.size-1)] str << chars[rand(chars.size - 1)]
end end
return str str
end end
# TODO: rikki? # TODO: rikki?
def Verification.contain params, filter def self.contain(params, filter)
(params.instance_of?(Array) ? params : params.keys).each do |key| params.instance_of?(Array) ? (filter - params).empty? : filter.all? { |s| params.key? s }
return false unless filter.include? key.to_sym end
end end
return true
end
end