Swiftpack.co -  tgymnich/OTPKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
tgymnich/OTPKit
πŸ” Swift package to generate HOTP and TOTP tokens and save them to the keychain
.package(url: "https://github.com/tgymnich/OTPKit.git", from: "1.2.0")

OTPKit

Swift codecov

A little swift package to generate HOTP and TOTP tokens and save them to the keychain. Featuring a combine publisher that publishes new tokens.

SwiftPM

// swift-tools-version:5.0
import PackageDescription

let package = Package(
  name: "MyTool",
  dependencies: [
    .package(url: "https://github.com/tgymnich/OTPKit.git", .from: "1.0.0"),
  ],
  targets: [
    .target(name: "MyTool", dependencies: ["OTPKit"]),
  ]
)

Features

  • Create TOTP and HOTP tokens πŸ”‘
  • NotificationCenter notifications for current tokens πŸ“»
  • Combine Publisher for newly current tokens πŸ“‘
  • Save and load accounts from Keychain πŸ”’

Usage

Accounts

Creating an Account from an URL

let url = URL(string: "otpauth://totp/foo?secret=wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j&algorithm=SHA256&digits=6&period=30")!
let account = Account<TOTP>(from: url)

print(account?.otpGenerator.code()) // Prints the TOTP code for the current time.

Saving and loading of accounts from the Keychain

let keychain = Keychain(service: "ch.gymni.test.otpauth")

let url = URL(string: "otpauth://totp/foo?secret=wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j&algorithm=SHA256&digits=6&period=30")!
let totp = TOTP(algorithm: .sha256, secret: "wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j".base32DecodedData! , digits: 6, period: 30)
let account = Account(label: "foo", otp: totp)
try account.save(to: keychain)

let accounts = try? Account.loadAll(from: keychain)

Deleting all accounts from keychain

let keychain = Keychain(service: "ch.gymni.test.otpauth")
try! keychain.removeAll()

Generating TOTP codes

For a specific Date

let date = Date(timeIntervalSince1970: 1234)
let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: 30)
let code = totp.code(for: date)

For the current period

let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: 30)
let code = totp.code()

For a custom period

let period: UInt64 = 1234
let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: 30)
let code = totp.code(for: period)

Using NotificationCenter

let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: 30)
NotificationCenter.default.addObserver(forName: .didGenerateNewOTPCode, object: totp, queue: .main) { notification in
   let code = notification.userInfo?[TOTP.UserInfoKeys.code] as? String
}

Using a Combine Publisher

let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: TOTPPublisherTests.period)
let pub = TOTP.TOTPPublisher(totp: totp)
    .sink { code in
        print(code)
   }

GitHub

link
Stars: 5
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

48 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API