Code Monkey home page Code Monkey logo

swiftotp's Introduction

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.

swiftotp's People

Contributors

dbarden avatar diegotl avatar hyerra avatar lachlanbell avatar manas-sharma-1683 avatar marcosgriselli avatar rkreutz avatar sai-manoj-kumar avatar trentrand avatar

Stargazers

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

Watchers

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

swiftotp's Issues

CryptoKit

Should investigate using CryptoKit on newer OSes instead of a 3rd-party dependency

Type of expression is ambiguous without more context

I just try this

TOTP(secret: getUser()?.secret, digits: 6, timeInterval: 30, algorithm: .sha1)

And swift hits me with

Type of expression is ambiguous without more context

Am i missing something? I was following the readme

New Release

Hey,

Would it be possible to create a new patch release for this project? We have SwiftOTP as a dependency in a project and would like to link it via SPM as a dynamic library.

Today we need to point it to master but we would like to be able to point to a tag :)

Let me know if there's anything I can help with.

Thanks in advance.

Undefined Symbol Error

When I add the package to my project, there are three undefined symbol errors:

Undefined symbol: _$s8SwiftOTP18base32DecodeToDatay10Foundation0F0VSgSSF

Undefined symbol: _$s8SwiftOTP4TOTPV6secret6digits12timeInterval9algorithmACSg10Foundation4DataV_S2iAA12OTPAlgorithmOtcfC

Undefined symbol: _$s8SwiftOTP4TOTPV8generate4timeSSSg10Foundation4DateV_tF

Would you know how can I resolve this?

SwiftOTP Kit without dependencies

Consider making separate package without any dependencies, just some abstract protocols for working with crypto stuff.
This would make possible to have SwiftOTP packages with dependencies to SwiftCrypto, OpenCrypto or CryptoKit (#16 might be related). Since Vapor 4 doesn't have TOTP replacement as of now, this library would be great for using in Vapor.
Right now I had to copy sources to my project and modify them directly.
Still library looks great, thanks!

Bitcode support

When I am trying to build a watchOS app using this library everything is working fine but after the upload to the App Store I get the following error as email.

TMS-90562: Invalid Bundle - Bitcode failed to compile for your watchOS binary because it includes assembly source code or inline assembly. To resolve this issue, rewrite the corresponding code in a higher-level language such as Objective-C or Swift and redeliver your app.

Is it possible to add bitcode support? For iOS I can disable the bitcode support, but for watchOS is it required.

Number of Digits

I notice it only accept range 6...8

I believe it should accept 4 since it is a common case

Carthage Support?

We can't use cocoapods for any projects in my company right now. Will this library be available through Carthage in the future?

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.