Last Updated: February 25, 2016
·
2.299K
· cjoudrey

Time-based one-time passwords for your app (Google Authenticator Compatible)

Using the rotp gem, you can generate one-time passwords that can be used for two-factor authentication for your application.

You can generate time-based one-time passwords:

totp = ROTP::TOTP.new("base32secret3232")
totp.now # => 492039

# OTP verified for current time
totp.verify(492039) # => true
sleep 30
# The password expires after 30 seconds
totp.verify(492039) # => false

You can also generate counter-based one-time passwords:

hotp = ROTP::HOTP.new("base32secretkey3232")
hotp.at(0) # => 260182
hotp.at(1) # => 55283
hotp.at(1401) # => 316439

# OTP verified with a counter
totp.verify(316439, 1401) # => true
totp.verify(316439, 1402) # => false

The gem can also generate Google Authenticator compatible URI's.

totp.provisioning_uri("alice@google.com") # => 'otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP'
hotp.provisioning_uri("alice@google.com", 0) # => 'otpauth://hotp/alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=0'

README.md