Method: ROTP::OTP#generate_otp

Defined in:
lib/rotp/otp.rb

#generate_otp(input) ⇒ Object

Usually either the counter, or the computed integer based on the Unix timestamp

Parameters:

  • input (Integer)

    the number used seed the HMAC


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rotp/otp.rb', line 35

def generate_otp(input)
  hmac = OpenSSL::HMAC.digest(
    OpenSSL::Digest.new(digest),
    byte_secret,
    int_to_bytestring(input)
  )

  offset = hmac[-1].ord & 0xf
  code = (hmac[offset].ord & 0x7f) << 24 |
         (hmac[offset + 1].ord & 0xff) << 16 |
         (hmac[offset + 2].ord & 0xff) << 8 |
         (hmac[offset + 3].ord & 0xff)
  code_str = (10 ** digits + (code % 10 ** digits)).to_s
  code_str[-digits..-1]
end