Class List Method List File List

# Class: ROTP::OTP

Inherits:
  Object
  - Object
  - ROTP::OTP
  show all
Defined in:
  lib/rotp/otp.rb

## Direct Known Subclasses

HOTP, TOTP

## 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) generate_otp(input, padded = true)

  Usually either the counter, or the computed integer based on the Unix timestamp.

- - (OTP) initialize(s, options = {}) constructor

  A new instance of OTP.

## Constructor Details

### - (`OTP`) initialize(s, options = {})

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

    13
    14
    15
    16
    17

    # File 'lib/rotp/otp.rb', line 13

    def initialize(s, options = {})
      @digits = options[:digits] || 6
      @digest = options[: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`) generate_otp(input, padded = true)

Usually either the counter, or the computed integer based on the Unix timestamp

Parameters:

- input (`Integer`) —

  the number used seed the HMAC

- padded (`Hash`) (defaults to: `true`) —

  a customizable set of options

Options Hash (`padded`):

- (false) (`Boolean`) —

  Output the otp as a 0 padded string

    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40

    # File 'lib/rotp/otp.rb', line 23

    def generate_otp(input, padded=true)
      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)
      if padded
        (code % 10 ** digits).to_s.rjust(digits, '0')
      else
        code % 10 ** digits
      end
    end