Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

1.2 KB
Raw Blame
import Foundation
import RustCoreFramework

/// Time-based One-Time Password (TOTP) codes, generated by the Rust core (`core/rust/src/totp`).
/// Other platform implementations: TotpUtility.ts (core/client), TotpUtility.kt (Android).
public class TotpUtility {
    /// Generate a TOTP code from a secret key.
    /// - Parameters:
    ///   - secret: The Base32-encoded secret key
    ///   - time: The current time (defaults to now)
    ///   - period: The time step in seconds (defaults to 30)
    ///   - digits: Number of digits in the code (defaults to 6)
    ///   - algorithm: The HMAC algorithm name: SHA1, SHA256 or SHA512 (defaults to SHA1)
    /// - Returns: The generated TOTP code, or nil if the secret is unusable
    public static func generateTotpCode(
        secret: String,
        time: Date = Date(),
        period: Int = 30,
        digits: Int = 6,
        algorithm: String = "SHA1"
    ) -> String? {
        return RustCoreFramework.generateTotpCode(
            secret: secret,
            unixSeconds: Int64(time.timeIntervalSince1970),
            algorithm: algorithm,
            digits: UInt32(clamping: digits),
            period: UInt32(clamping: period)
        )
    }
}