Class: Mintotp::TOTP
- Inherits:
-
Object
- Object
- Mintotp::TOTP
- Defined in:
- lib/mintotp/totp.rb
Instance Method Summary collapse
- #hotp(key, counter, digits = 6, digest = 'sha1') ⇒ Object
-
#totp(key, time_step = 30, digits = 6, digest = 'sha1') ⇒ string
If so.
Instance Method Details
#hotp(key, counter, digits = 6, digest = 'sha1') ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mintotp/totp.rb', line 21 def hotp(key, counter, digits=6, digest='sha1') key = Base32.decode "#{key}#{"="* ((8-key.length)%8)}" sha = OpenSSL::Digest::Digest.new(digest) counter = [counter].pack('Q>') hmac= OpenSSL::HMAC.digest(sha, key, counter) offset = hmac.bytes.last & 0x0f bytes = hmac.bytes[offset..offset+4] # unpack by bytes shifting bytes[0] = bytes[0] & 0x7f bytes_as_integer = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3] bytes_as_integer.modulo(10 ** digits).to_s.rjust(digits,'0') end |
#totp(key, time_step = 30, digits = 6, digest = 'sha1') ⇒ string
Returns if so.
17 18 19 |
# File 'lib/mintotp/totp.rb', line 17 def totp(key, time_step=30, digits=6, digest='sha1') hotp(key, Time.now.to_i / time_step, digits, digest) end |