Sha256: a0338dcc5a1af6b9edc66212f3bbeac415cdd216ccdad34374e6a85e6c0ebce6

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

module ROTP
  class HOTP < OTP
    # Generates the OTP for the given count
    # @param [Integer] count counter
    # @option [Boolean] padding (false) Issue the number as a 0 padded string
    # @returns [Integer] OTP
    def at(count, padding=true)
      generate_otp(count, padding)
    end

    # Verifies the OTP passed in against the current time OTP
    # @param [String/Integer] otp the OTP to check against
    # @param [Integer] counter the counter of the OTP
    def verify(otp, counter)
      super(otp, self.at(counter))
    end

    # Verifies the OTP passed in against the current time OTP, with a given number of retries.
    # Returns the counter that was verified successfully
    # @param [String/Integer] otp the OTP to check against
    # @param [Integer] initial counter the counter of the OTP
    # @param [Integer] number of retries
    def verify_with_retries(otp, initial_count, retries = 1)
      return false if retries <= 0

      1.upto(retries) do |counter|
        current_counter = initial_count + counter
        return current_counter if verify(otp, current_counter)
      end

      false
    end

    # Returns the provisioning URI for the OTP
    # This can then be encoded in a QR Code and used
    # to provision the Google Authenticator app
    # @param [String] name of the account
    # @param [Integer] initial_count starting counter value, defaults to 0
    # @return [String] provisioning uri
    def provisioning_uri(name, initial_count=0)
      encode_params("otpauth://hotp/#{URI.encode(name)}", :secret=>secret, :counter=>initial_count)
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rotp-3.0.1 lib/rotp/hotp.rb
rotp-3.0.0 lib/rotp/hotp.rb
rotp-2.1.2 lib/rotp/hotp.rb
rotp-2.1.1 lib/rotp/hotp.rb
rotp-2.1.0 lib/rotp/hotp.rb
rotp-2.0.0 lib/rotp/hotp.rb