Giter Club home page Giter Club logo

otpkit's Introduction

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)
   }

otpkit's People

Contributors

dependabot[bot] avatar tg908 avatar tgymnich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.