All Projects → lachlanbell → Swiftotp

lachlanbell / Swiftotp

Licence: mit
A Swift library for generating One Time Passwords (OTP)

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftotp

Aegis
A free, secure and open source app for Android to manage your 2-step verification tokens.
Stars: ✭ 2,692 (+2162.18%)
Mutual labels:  2fa, totp, hotp
Go Guardian
Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to create powerful modern API and web authentication.
Stars: ✭ 204 (+71.43%)
Mutual labels:  2fa, totp, hotp
One Time
One Time Password (TOTP and HOTP) library for Clojure. TOTP/HOTP is widely used for Two Factor / Multi Factor Authentication.
Stars: ✭ 129 (+8.4%)
Mutual labels:  2fa, totp, hotp
Java Otp
A one-time password (HOTP/TOTP) library for Java
Stars: ✭ 265 (+122.69%)
Mutual labels:  2fa, totp, hotp
otp-java
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).
Stars: ✭ 107 (-10.08%)
Mutual labels:  totp, hotp, 2fa
pyotp
Python One-Time Password Library
Stars: ✭ 1,930 (+1521.85%)
Mutual labels:  totp, hotp, 2fa
Authenticatorpro
📱 Two-Factor Authentication (2FA) client for Android + Wear OS
Stars: ✭ 155 (+30.25%)
Mutual labels:  2fa, totp, hotp
Freeotpplus
Enhanced fork of FreeOTP-Android providing a feature-rich 2FA authenticator
Stars: ✭ 223 (+87.39%)
Mutual labels:  2fa, totp, hotp
crotp
CrOTP - One Time Passwords for Crystal
Stars: ✭ 62 (-47.9%)
Mutual labels:  totp, hotp, 2fa
Onetimepassword
🔑 A small library for generating TOTP and HOTP one-time passwords on iOS.
Stars: ✭ 243 (+104.2%)
Mutual labels:  2fa, totp, hotp
2FAuth
A Web app to manage your Two-Factor Authentication (2FA) accounts and generate their security codes
Stars: ✭ 664 (+457.98%)
Mutual labels:  totp, hotp, 2fa
apache 2fa
Apache two-factor (2FA) authentication with Google Authenticator based on Time-based One-Time Password (TOTP) or HMAC-based one-time password (HOTP) Algorithms.
Stars: ✭ 63 (-47.06%)
Mutual labels:  totp, hotp, 2fa
Mintotp
Minimal TOTP generator in 20 lines of Python
Stars: ✭ 678 (+469.75%)
Mutual labels:  2fa, totp, hotp
OneTime
iOS, watchOS, & macOS One-Time Password client
Stars: ✭ 14 (-88.24%)
Mutual labels:  totp, hotp
extract otp secret keys
Extract two-factor authentication (2FA, TFA) secret keys from export QR codes of "Google Authenticator" app
Stars: ✭ 217 (+82.35%)
Mutual labels:  totp, 2fa
2FAtoTray
 Copy 2FA tokens in a click (macOS)
Stars: ✭ 13 (-89.08%)
Mutual labels:  totp, 2fa
Andotp
Open source two-factor authentication for Android
Stars: ✭ 3,326 (+2694.96%)
Mutual labels:  totp, hotp
Otphp
🔐 A PHP library for generating one time passwords according to RFC 4226 (HOTP) and the RFC 6238 (TOTP)
Stars: ✭ 857 (+620.17%)
Mutual labels:  totp, hotp
Otplib
🔑 One Time Password (OTP) / 2FA for Node.js and Browser - Supports HOTP, TOTP and Google Authenticator
Stars: ✭ 916 (+669.75%)
Mutual labels:  2fa, hotp
Jsotp
Javascript One-Time Password module.
Stars: ✭ 71 (-40.34%)
Mutual labels:  totp, hotp

Logo SwiftOTP

Build Status Version Carthage compatible License Platform Swift Version

SwiftOTP 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 and RFC 6238 respectively.

Installation

CocoaPods

SwiftOTP is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftOTP'

Then run pod install in the project directory to install.

Carthage

SwiftOTP is available through Carthage. To install it, simply add the following line to your Cartfile:

github "lachlanbell/SwiftOTP"

Then run carthage update in the project directory and add the resulting frameworks to your project.

Swift Package Manager

You can use Swift Package Manager and specify dependency in Package.swift by adding this:

dependencies: [
    .package(url: "https://github.com/lachlanbell/SwiftOTP.git", .upToNextMinor(from: "3.0.0"))
]

Usage

TOTP (Time-Based One Time Password)

Creation of a TOTP Object:

let totp = TOTP(secret: data)

A 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:

let totp = TOTP(secret: data, digits: 6, timeInterval: 30, algorithm: .sha1)

Generating TOTP Passwords

After 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

For example, to get a password for the current time using a TOTP object named totp:

if let totp = TOTP(secret: data) {
    let otpString = totp.generate(time: Date)
}

Or from Unix time (i.e. seconds elapsed since 01 Jan 1970 00:00 UTC):

if let totp = TOTP(secret: data) {
    let otpString = totp.generate(secondsPast1970: 1234567890)
}

Note: only Int values are accepted by this function, and must be positive.

HOTP (HMAC-Based One Time Password (counter-based))

In addition to TOTP, SwiftOTP also supports the generation of counter-based HOTP passwords.

Creation of an HOTP Object:

let hotp = HOTP(secret: data)

A 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:

let hotp = HOTP(secret: data, digits: 6, algorithm: .sha1)

Generating HOTP Passwords

After 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):

if let hotp = HOTP(secret: data) {
    let otpString = hotp.generate(counter: 42)
}

Base32

Most secret keys for generating one time passwords use Base32 encoding. As such, SwiftOTP includes a Base32 Helper to decode a Base32 string to Data.

For example:

base32DecodeToData("ABCDEFGHIJKLMNOP")!

Or in use:

guard let data = base32DecodeToData("ABCDEFGHIJKLMNOP") else { return }

if let hotp = HOTP(secret: data) {
    print(hotp.generate(42))
}

Supported parameters

Hash Functions

SwiftOTP supports HMAC with SHA1 as specified in RFC 4226, as well as SHA256 and SHA512 added in RFC 6238. MD5 is not supported, due to its insufficient hash length.

Digit Length

Both the TOTP and HOTP objects only accept a digit length value between 6 and 8, as specified in RFC 4226. Both objects will be nil if an invalid digit length value is provided.

Older Swift Versions

Use the corresponding branch for using an older Swift version (4.0 and greater). For example:

pod 'SwiftOTP', :git => 'https://github.com/lachlanbell/SwiftOTP.git', :branch => 'swift-4.0'

License

SwiftOTP is available under the MIT license. See the LICENSE file for more info.

Acknowledgements

SwiftOTP depends on the following open-source projects:

Some parts of the password generator code were adapted from the old Google Authenticator source.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].