{"id":13683664,"url":"https://github.com/lachlanbell/SwiftOTP","last_synced_at":"2025-04-30T13:31:38.046Z","repository":{"id":29233423,"uuid":"117502027","full_name":"lachlanbell/SwiftOTP","owner":"lachlanbell","description":"A Swift library for generating One Time Passwords (OTP)","archived":false,"fork":false,"pushed_at":"2025-02-12T10:24:45.000Z","size":114,"stargazers_count":294,"open_issues_count":4,"forks_count":61,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-08T22:34:36.907Z","etag":null,"topics":["2fa","hotp","ios","macos","swift","totp"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lachlanbell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-01-15T05:41:04.000Z","updated_at":"2025-03-31T02:38:44.000Z","dependencies_parsed_at":"2024-04-20T17:04:15.834Z","dependency_job_id":null,"html_url":"https://github.com/lachlanbell/SwiftOTP","commit_stats":{"total_commits":78,"total_committers":10,"mean_commits":7.8,"dds":"0.17948717948717952","last_synced_commit":"f8ba33ca419e9e9488b1d23afccc7fca9d4f5f29"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lachlanbell%2FSwiftOTP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lachlanbell%2FSwiftOTP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lachlanbell%2FSwiftOTP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lachlanbell%2FSwiftOTP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lachlanbell","download_url":"https://codeload.github.com/lachlanbell/SwiftOTP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251712896,"owners_count":21631461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","hotp","ios","macos","swift","totp"],"created_at":"2024-08-02T13:02:21.614Z","updated_at":"2025-04-30T13:31:37.785Z","avatar_url":"https://github.com/lachlanbell.png","language":"Swift","readme":"# \u003cimg src=\"https://user-images.githubusercontent.com/19360256/34930442-5ed005d4-fa04-11e7-8aea-44179368fcde.png\" alt=\"Logo\" width=\"36\" height=\"36\"\u003e SwiftOTP\n\n[![Build Status](https://www.travis-ci.com/lachlanbell/SwiftOTP.svg?branch=master)](https://www.travis-ci.com/lachlanbell/SwiftOTP)\n[![Version](https://img.shields.io/cocoapods/v/SwiftOTP.svg?style=flat)](http://cocoapods.org/pods/SwiftOTP)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![License](https://img.shields.io/cocoapods/l/SwiftOTP.svg?style=flat)](http://cocoapods.org/pods/SwiftOTP)\n[![Platform](https://img.shields.io/badge/platform-ios%20%7C%20macos%20%7C%20watchos%20%7C%20tvos%20%7C%20linux-lightgrey)](http://cocoapods.org/pods/SwiftOTP)\n![Swift Version](https://img.shields.io/badge/Swift-5.0-orange.svg)\n\nSwiftOTP is a Swift library for generating One Time Passwords (OTP) commonly used for two factor authentication. SwiftOTP supports both HMAC-Based One Time Passwords (HOTP) and Time Based One Time Passwords (TOTP) defined in [RFC 4226](https://tools.ietf.org/html/rfc4226) and [RFC 6238](https://tools.ietf.org/html/rfc6238) respectively.\n## Installation\n### CocoaPods\nSwiftOTP is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:\n\n```ruby\npod 'SwiftOTP'\n```\nThen run `pod install` in the project directory to install.\n\n### Carthage\nSwiftOTP is available through [Carthage](https://github.com/Carthage/Carthage). To install it, simply add the following line to your Cartfile:\n\n```\ngithub \"lachlanbell/SwiftOTP\"\n```\nThen run `carthage update` in the project directory and add the resulting frameworks to your project.\n\n### Swift Package Manager\nYou can use [Swift Package Manager](https://swift.org/package-manager/) and specify dependency in `Package.swift` by adding this:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/lachlanbell/SwiftOTP.git\", .upToNextMinor(from: \"3.0.0\"))\n]\n```\n\n## Usage\n### TOTP (Time-Based One Time Password)\n\n#### Creation of a TOTP Object:\n```swift\nlet totp = TOTP(secret: data)\n```\nA TOTP Object can be created with the default settings (6 digits, 30sec time interval and using HMAC-SHA-1) as shown above, or the individual parameters can be set as shown below:\n```swift\nlet totp = TOTP(secret: data, digits: 6, timeInterval: 30, algorithm: .sha1)\n```\n#### Generating TOTP Passwords\nAfter creating a TOTP object, a password can be generated for a point in time, either by using a `Date` object or a Unix time value using the `generate()` function\n\nFor example, to get a password for the current time using a `TOTP` object named `totp`:\n\n```swift\nif let totp = TOTP(secret: data) {\n    let otpString = totp.generate(time: Date)\n}\n```\n\n\nOr from Unix time (i.e. seconds elapsed since 01 Jan 1970 00:00 UTC):\n```swift\nif let totp = TOTP(secret: data) {\n    let otpString = totp.generate(secondsPast1970: 1234567890)\n}\n```\nNote: only `Int` values are accepted by this function, and must be positive.\n\n### HOTP (HMAC-Based One Time Password (counter-based))\n\nIn addition to TOTP, SwiftOTP also supports the generation of counter-based HOTP passwords.\n#### Creation of an HOTP Object:\n```swift\nlet hotp = HOTP(secret: data)\n```\nA HOTP Object can be created with the default settings (6 digits, using HMAC-SHA-1) as shown above, or the individual parameters can be set as shown below:\n```swift\nlet hotp = HOTP(secret: data, digits: 6, algorithm: .sha1)\n```\n#### Generating HOTP Passwords\nAfter creating a HOTP object, a password can be generated for a counter value (`UInt64`) by using the `generate()` function, for example (where `hotp` is a `HOTP` object):\n```swift\nif let hotp = HOTP(secret: data) {\n    let otpString = hotp.generate(counter: 42)\n}\n```\n\n### Base32\nMost secret keys for generating one time passwords use Base32 encoding. As such, SwiftOTP includes a [Base32 Helper](https://github.com/norio-nomura/Base32) to decode a Base32 string to `Data`.\n\nFor example:\n```swift\nbase32DecodeToData(\"ABCDEFGHIJKLMNOP\")!\n```\n\nOr in use:\n```swift\nguard let data = base32DecodeToData(\"ABCDEFGHIJKLMNOP\") else { return }\n\nif let hotp = HOTP(secret: data) {\n    print(hotp.generate(42))\n}\n```\n\n### Supported parameters\n#### Hash Functions\nSwiftOTP supports HMAC with SHA1 as specified in [RFC 4226](https://tools.ietf.org/html/rfc4226), as well as SHA256 and SHA512 added in [RFC 6238](https://tools.ietf.org/html/rfc6238). MD5 is **not** supported, due to its insufficient hash length.\n\n#### Digit Length\nBoth the `TOTP` and `HOTP` objects only accept a digit length value between 6 and 8, as specified in [RFC 4226](https://tools.ietf.org/html/rfc4226). Both objects will be `nil` if an invalid digit length value is provided.\n\n## Older Swift Versions\nUse the corresponding branch for using an older Swift version (4.0 and greater). For example:\n```ruby\npod 'SwiftOTP', :git =\u003e 'https://github.com/lachlanbell/SwiftOTP.git', :branch =\u003e 'swift-4.0'\n```\n\n## License\nSwiftOTP is available under the MIT license. See the LICENSE file for more info.\n\n### Acknowledgements\nSwiftOTP depends on the following open-source projects:\n\n* [CryptoSwift](https://github.com/krzyzanowskim/CryptoSwift) by Marcin Krzyżanowski ([License](https://github.com/krzyzanowskim/CryptoSwift/tree/master/LICENSE))\n* [Base32](https://github.com/norio-nomura/Base32) by Norio Nomura ([License](https://github.com/norio-nomura/Base32/blob/master/LICENSE))\n\nSome parts of the password generator code were adapted from the [old Google Authenticator source](https://github.com/google/google-authenticator).\n","funding_links":[],"categories":["Swift"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flachlanbell%2FSwiftOTP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flachlanbell%2FSwiftOTP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flachlanbell%2FSwiftOTP/lists"}