Class: ROTP::OTP

Inherits:
Object
  • Object
show all
Defined in:
lib/rotp/otp.rb,
lib/rotp/otp/uri.rb

Direct Known Subclasses

HOTP, TOTP

Defined Under Namespace

Classes: URI

Constant Summary collapse

DEFAULT_DIGITS =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, options = {}) ⇒ OTP

Returns a new instance of OTP.

Parameters:

  • secret (String)

    in the form of base32

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • digits (Integer) — default: 6

    Number of integers in the OTP. Google Authenticate only supports 6 currently

  • digest (String) — default: sha1

    Digest used in the HMAC. Google Authenticate only supports ‘sha1’ currently

  • name (String)

    The name of the account for the OTP. Used in the provisioning URL

  • issuer (String)

    The issuer of the OTP. Used in the provisioning URL

  • provisioning_params (Hash) — default: {}

    Additional non-standard params you may want appended to the provisioning URI. Ex. ‘image: ’‘`



23
24
25
26
27
28
29
30
# File 'lib/rotp/otp.rb', line 23

def initialize(s, options = {})
  @digits = options[:digits] || DEFAULT_DIGITS
  @digest = options[:digest] || 'sha1'
  @name = options[:name]
  @issuer = options[:issuer]
  @provisioning_params = options[:provisioning_params] || {}
  @secret = s
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



3
4
5
# File 'lib/rotp/otp.rb', line 3

def digest
  @digest
end

#digitsObject (readonly)

Returns the value of attribute digits.



3
4
5
# File 'lib/rotp/otp.rb', line 3

def digits
  @digits
end

#issuerObject (readonly)

Returns the value of attribute issuer.



3
4
5
# File 'lib/rotp/otp.rb', line 3

def issuer
  @issuer
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rotp/otp.rb', line 3

def name
  @name
end

#provisioning_paramsObject (readonly)

Returns the value of attribute provisioning_params.



3
4
5
# File 'lib/rotp/otp.rb', line 3

def provisioning_params
  @provisioning_params
end

#secretObject (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

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