A package for generating HMAC-based and Time-based one-time passwords.
See the full documentation.
You can create a generator using an URL string:
let totp = try TOTP(urlString: "otpauth://totp/?secret=JBSWY3DPEHPK3PXP")Alternatively, you can pass all of the parameters:
let hotp = try HOTP(secret: [0x41, 0x41, 0x43], counter: 0, digits: 8)To create a one-time password code, use the generateCode() method:
let code = try totp.generateCode()If you are using the TOTP generator, you can validate a code accepting a number of previous and next codes:
let isValid = try totp.validate("123456", acceptPreviousCodes: 2, acceptNextCodes: 1)For TOTP, you can also use a for loop over the async codes method:
let task = Task {
for try await code in totp.codes {
print("New code:", code.code)
}
}For details on how to use the package with SwiftUI, check the tutorial: Use with SwiftUI
OneTimePass is released under the 2-Clause BSD License. See LICENSE for details.