Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tgymnich/otpkit
🔐 Swift package to generate HOTP and TOTP tokens and save them to the keychain
https://github.com/tgymnich/otpkit
2fa hotp keychain otp package swift totp totp-tokens
Last synced: 2 months ago
JSON representation
🔐 Swift package to generate HOTP and TOTP tokens and save them to the keychain
- Host: GitHub
- URL: https://github.com/tgymnich/otpkit
- Owner: tgymnich
- License: mit
- Created: 2019-07-22T23:32:03.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-02T21:07:32.000Z (10 months ago)
- Last Synced: 2024-03-02T22:23:12.707Z (10 months ago)
- Topics: 2fa, hotp, keychain, otp, package, swift, totp, totp-tokens
- Language: Swift
- Homepage:
- Size: 105 KB
- Stars: 10
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OTPKit
![Swift](https://github.com/TG908/OTPKit/workflows/Swift/badge.svg)
[![codecov](https://codecov.io/gh/tgymnich/OTPKit/branch/master/graph/badge.svg?token=TALY1DNJWN)](https://codecov.io/gh/tgymnich/OTPKit)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
// swift-tools-version:5.0
import PackageDescriptionlet 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
```swift
let url = URL(string: "otpauth://totp/foo?secret=wew3k6ztd7kuh5ucg4pejqi4swwrrneh72ad2sdovikfatzbc5huto2j&algorithm=SHA256&digits=6&period=30")!
let account = Account(from: url)print(account?.otpGenerator.code()) // Prints the TOTP code for the current time.
```### Saving and loading of accounts from the Keychain
```swift
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
```swift
let keychain = Keychain(service: "ch.gymni.test.otpauth")
try! keychain.removeAll()
```## Generating TOTP codes
### For a specific Date
```swift
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
```swift
let totp = TOTP(algorithm: .sha256, secret: "01234567890".data(using: .ascii)!, digits: 6, period: 30)
let code = totp.code()
```### For a custom period
```swift
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
```swift
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
```swift
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)
}
```