Module: Motp

Defined in:
lib/motp.rb,
lib/motp/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.check(secret, pin, otp, options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/motp.rb', line 9

def self.check(secret, pin, otp, options = {})
  options = {:time => Time::now, :max_period => (3 * 60)}.merge(options)
  range = ((options[:time] - options[:max_period])..(options[:time] + options[:max_period]))
  return range.any?{|time| otp == self.otp(secret, pin, :time => time)}
end

.otp(secret, pin, options = {}) ⇒ Object



4
5
6
7
# File 'lib/motp.rb', line 4

def self.otp(secret, pin, options = {})
  options = {:time => Time::now}.merge(options)
  Digest::MD5::hexdigest("#{options[:time].utc.tv_sec.to_s[0...-1]}#{secret}#{pin}")[0,6]
end