ensl.org/lib/verification.rb

63 lines
1.3 KiB
Ruby
Raw Normal View History

module Verification
2015-11-03 08:41:25 +00:00
def self.verify(input)
md5 = Digest::MD5.hexdigest("9WvcZ9hX" + input + "KF7L4luQ").upcase.split(//)
chars = %w[A B C D E F]
nums = []
last_pos = md5[31].to_i
result = ""
(0..9).each do |i|
pos = md5[i].to_i
if pos == 0
pos = last_pos**(i % 4)
elsif (pos % 4) == 0
pos = pos * last_pos + i
elsif (pos % 3) == 0
pos **= (i % 4)
elsif pos.even?
pos = pos * i + pos
end
pos = (pos > 31) ? (pos % 32) : pos
cur_char = md5[31 - pos]
cur_num = cur_char.to_i
if nums.include? cur_num
if cur_num == 0
cur_char = chars[pos % 6]
else
cur_char = (pos % 10).to_s
end
cur_num = cur_char.to_i
end
nums << cur_num
result << cur_char
last_pos = pos
end
result
end
def self.uncrap(str)
str.to_s.gsub(/[^A-Za-z0-9_\-]/, "")
end
def self.random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
str = ""
1.upto(len) do
str << chars[rand(chars.size - 1)]
end
str
end
# TODO: rikki?
# Returns true if params (or its keys if a hash) are a subset of filter
2015-11-03 08:41:25 +00:00
def self.contain(params, filter)
2015-11-08 09:41:11 +00:00
keys = (params.instance_of?(Array) ? params : params.keys).map(&:to_sym)
(keys - filter).empty?
2015-11-03 08:41:25 +00:00
end
end