Class: ROTP::OTP
- Inherits:
-
Object
- Object
- ROTP::OTP
- Defined in:
- lib/rotp/otp.rb
Instance Attribute Summary (collapse)
-
- (Object) digest
readonly
Returns the value of attribute digest.
-
- (Object) digits
readonly
Returns the value of attribute digits.
-
- (Object) secret
readonly
Returns the value of attribute secret.
Instance Method Summary (collapse)
- - (Object) byte_secret
- - (Object) generate_otp(count)
-
- (OTP) initialize(s, options = {})
constructor
Params: secret in base32.
- - (Object) int_to_bytestring(int, padding = 8)
Constructor Details
- (OTP) initialize(s, options = {})
Params: secret in base32
6 7 8 9 10 |
# File 'lib/rotp/otp.rb', line 6 def initialize(s, = {}) @digits = [:digits] || 6 @digest = [:digest] || "sha1" @secret = s end |
Instance Attribute Details
- (Object) digest (readonly)
Returns the value of attribute digest
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def digest @digest end |
- (Object) digits (readonly)
Returns the value of attribute digits
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def digits @digits end |
- (Object) secret (readonly)
Returns the value of attribute secret
3 4 5 |
# File 'lib/rotp/otp.rb', line 3 def secret @secret end |
Instance Method Details
- (Object) byte_secret
27 28 29 |
# File 'lib/rotp/otp.rb', line 27 def byte_secret Base32.decode(@secret) end |
- (Object) generate_otp(count)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rotp/otp.rb', line 12 def generate_otp(count) hmac = OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new(digest), byte_secret, int_to_bytestring(count) ) offset = hmac[19] & 0xf code = (hmac[offset] & 0x7f) << 24 | (hmac[offset + 1] & 0xff) << 16 | (hmac[offset + 2] & 0xff) << 8 | (hmac[offset + 3] & 0xff) code % 10 ** digits end |
- (Object) int_to_bytestring(int, padding = 8)
31 32 33 34 35 36 37 38 |
# File 'lib/rotp/otp.rb', line 31 def int_to_bytestring(int, padding = 8) result = [] until int == 0 result << (int & 0xFF).chr int >>= 8 end result.reverse.join.rjust(8, 0.chr) end |