Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🔐 Swift package to generate HOTP and TOTP tokens and save them to the keychain

License

NotificationsYou must be signed in to change notification settings

tgymnich/OTPKit

Repository files navigation

Swiftcodecov

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.0import PackageDescriptionletpackage=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

leturl=URL(string:"otpauth://totp/foo?secret=wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j&algorithm=SHA256&digits=6&period=30")!letaccount=Account<TOTP>(from: url)print(account?.otpGenerator.code()) // Prints the TOTP code for the current time.

Saving and loading of accounts from the Keychain

letkeychain=Keychain(service:"ch.gymni.test.otpauth")leturl=URL(string:"otpauth://totp/foo?secret=wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j&algorithm=SHA256&digits=6&period=30")!lettotp=TOTP(algorithm:.sha256, secret:"wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j".base32DecodedData!, digits:6, period:30)letaccount=Account(label:"foo", otp: totp)try account.save(to: keychain)letaccounts=try?Account.loadAll(from: keychain)

Deleting all accounts from keychain

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

Generating TOTP codes

For a specific Date

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

For the current period

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

For a custom period

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

Using NotificationCenter

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

Using a Combine Publisher

lettotp=TOTP(algorithm:.sha256, secret:"01234567890".data(using:.ascii)!, digits:6, period:TOTPPublisherTests.period)letpub=TOTP.TOTPPublisher(totp: totp).sink{ codeinprint(code)}

About

🔐 Swift package to generate HOTP and TOTP tokens and save them to the keychain

Topics

Resources

License

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp