Skip to content
/ OTPKit Public

A small library written in Swift for generation time-based one-time passwords

Notifications You must be signed in to change notification settings

belous/OTPKit

Repository files navigation

OTPKit

A small library written in Swift which can generate time-based one-time passwords.

Swift codecov CodeFactor

Installation

  • In Package.swift add the following:
dependencies: [
    .package(url: "https://github.com/belous/OTPKit.git", from: "0.3.0"),
],
targets: [
    .target(
        name: "MainApp",
        dependencies: ["OTPKit"]
    )
    
]

Usage

Create a struct or class and confirm it to the OTPProvidable protocol:

public struct Secret: Codable, Equatable, Hashable {
    public let secret: String
    public let digits: Int
    public let movingFactor: MovingFactor
    public let hmacAlgorithm: HMACAlgorithm

    public init(secret: String,
                digits: Int = 6,
                movingFactor: MovingFactor = .timer(period: 30),
                hmacAlgorithm: HMACAlgorithm = .sha1) {
        self.secret = secret
        self.digits = digits
        self.movingFactor = movingFactor
        self.hmacAlgorithm = hmacAlgorithm
    }
}

extension Secret: OTPProvidable {}

To generate the current password you can call func getOTP(for: date) on instance of your's OTPProvidable:

let secret = Secret(secret: "AABCDEFABCDEFABC")
let currentPassword = secret.getOTP(for: Date())

License

This project is made available under the terms of the MIT License.