TotpGeneratorContext

public final class TotpGeneratorContext : OtpGeneratorContext

A class of Generator used to generate a time based one-time password (TOTP).

  • A value between 10 and 300 representing the number of seconds the password is valid. Default is 30.

    Declaration

    Swift

    fileprivate(set) public var period: TimeInterval
  • Construct an instance containing the given secret, password length, algorithm and period the password is valid for. Initialization returns nil if digits length is invalid or period outside the range.

    Declaration

    Swift

    public init?(secret: String, digits: Int = 6, algorithm: HmacAlgorithm = HmacAlgorithm.sha1, period: TimeInterval = 30)

    Parameters

    secret

    The secret key used to generate the password.

    digits

    A value of 6 or 8 representing the length of the password to be generated. The default is 6.

    algorithm

    The algorithm used to generate the password. The default is HmacAlgorithm.SHA1.

    period

    A value between 10 and 300 representing the number of seconds the password is valid. Default is 30.

  • This method generates a time based one-time password valid for a given period.

    Declaration

    Swift

    public func create(_ timeInterval: TimeInterval = Date().timeIntervalSince1970) -> String?

    Parameters

    timeInterval

    A date used to generate the one-time password. Default is now.

    Return Value

    Generated token or nil.

  • Calculates the remaining time for a given period in seconds, based on GMT: Thu, 01 Jan 1970 00:00:00 GMT.

    Declaration

    Swift

    public static func remainingTime(_ timeInterval: TimeInterval = 30) -> Int?

    Parameters

    timeInterval

    The period in seconds where the remaining time is needed, has to be > 0.

    Return Value

    The remaining time in seconds or nil.