OathCodeInfo
public struct OathCodeInfo : Codable, Sendable
Contains OTP code information, including the actual code and validity details.
This class provides comprehensive information about a generated OTP code, including timing information for TOTP codes and counter information for HOTP codes.
-
The generated OTP code.
Declaration
Swift
public let code: String -
For TOTP, the time remaining in seconds before the code expires. For HOTP, this will be -1.
Declaration
Swift
public let timeRemaining: Int -
For HOTP, the current counter value after code generation. For TOTP, this will be -1.
Declaration
Swift
public let counter: Int -
For TOTP, a value from 0.0 to 1.0 indicating progress through the time window. For HOTP, this will be 0.0.
Declaration
Swift
public let progress: Double -
For TOTP, the total validity period in seconds. For HOTP, this will be 0.
Declaration
Swift
public let totalPeriod: Int
-
Creates an instance for a TOTP code.
Declaration
Swift
public static func forTotp( code: String, timeRemaining: Int, totalPeriod: Int ) -> OathCodeInfoParameters
codeThe generated OTP code.
timeRemainingThe time remaining in seconds before the code expires.
totalPeriodThe total validity period in seconds.
Return Value
An OathCodeInfo instance configured for TOTP.
-
Creates an instance for a HOTP code.
Declaration
Swift
public static func forHotp( code: String, counter: Int ) -> OathCodeInfoParameters
codeThe generated OTP code.
counterThe counter value after code generation.
Return Value
An OathCodeInfo instance configured for HOTP.
-
Converts this code info to a JSON string representation.
This is a convenience method for cross-platform API consistency. You can also use Swift’s standard
JSONEncoderdirectly:let encoder = JSONEncoder() let data = try encoder.encode(codeInfo) let jsonString = String(data: data, encoding: .utf8)Throws
EncodingErrorif serialization fails.Declaration
Swift
public func toJson() throws -> StringReturn Value
A JSON string representing this code info.
-
Creates an OathCodeInfo from a JSON string.
This is a convenience method for cross-platform API consistency. You can also use Swift’s standard
JSONDecoderdirectly:let decoder = JSONDecoder() let codeInfo = try decoder.decode(OathCodeInfo.self, from: jsonData)Throws
DecodingErrorif the JSON is invalid.Declaration
Swift
public static func fromJson(_ jsonString: String) throws -> OathCodeInfoParameters
jsonStringThe JSON string to parse.
Return Value
An OathCodeInfo instance.
View on GitHub