Class: ROTP::OTP
- Inherits:
-
Object
- Object
- ROTP::OTP
- Defined in:
- lib/rotp/otp.rb,
lib/rotp/otp/uri.rb
Defined Under Namespace
Classes: URI
Constant Summary collapse
- DEFAULT_DIGITS =
6
Instance Attribute Summary collapse
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#digits ⇒ Object
readonly
Returns the value of attribute digits.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
-
#generate_otp(input) ⇒ Object
Usually either the counter, or the computed integer based on the Unix timestamp.
-
#initialize(s, options = {}) ⇒ OTP
constructor
A new instance of OTP.
Constructor Details
#initialize(s, options = {}) ⇒ OTP
Returns a new instance of OTP.
14 15 16 17 18 |
# File 'lib/rotp/otp.rb', line 14 def initialize(s, = {}) @digits = [:digits] || DEFAULT_DIGITS @digest = [:digest] || 'sha1' @secret = s end |
Instance Attribute Details
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def digest @digest end |
#digits ⇒ Object (readonly)
Returns the value of attribute digits.
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def digits @digits end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def secret @secret end |
Instance Method Details
#generate_otp(input) ⇒ Object
Usually either the counter, or the computed integer based on the Unix timestamp
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rotp/otp.rb', line 24 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 % 10**digits).to_s.rjust(digits, '0') end |