module OTP::Utils
Private Instance Methods
otp(algorithm, secret, moving_factor, digits)
click to toggle source
# File lib/otp/utils.rb, line 7 def otp(algorithm, secret, moving_factor, digits) message = otp_pack_int64(moving_factor) digest = otp_hmac(algorithm, secret, message) num = otp_pickup(digest) return otp_truncate(num, digits) end
otp_compare(a, b)
click to toggle source
# File lib/otp/utils.rb, line 36 def otp_compare(a, b) return a.to_i == b.to_i end
otp_hmac(algorithm, secret, text)
click to toggle source
# File lib/otp/utils.rb, line 18 def otp_hmac(algorithm, secret, text) mac = OpenSSL::HMAC.new(secret, algorithm) mac << text return mac.digest end
otp_pack_int64(i)
click to toggle source
# File lib/otp/utils.rb, line 14 def otp_pack_int64(i) return [i >> 32 & 0xffffffff, i & 0xffffffff].pack("NN") end
otp_pickup(digest)
click to toggle source
# File lib/otp/utils.rb, line 24 def otp_pickup(digest) offset = digest[-1].ord & 0xf binary = digest[offset, 4] return binary.unpack("N")[0] & 0x7fffffff end
otp_truncate(num, digits)
click to toggle source
# File lib/otp/utils.rb, line 30 def otp_truncate(num, digits) pw = (num % (10 ** digits)).to_s pw.prepend("0") while pw.length < digits return pw end