Skip to content

yazid/iOS-TOTP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iOS TOTP Sample

TOTP (Time-based One-time Password) is a great and simple approach for two-factor authentication. Google Authenticator uses it. But, the project for the original Google Authenticator iOS client is super bloated, and most likely won't compile on first run (at least it didn't for me). So, here's a barebones single-view iOS app which makes use of only 4 of the files from Google. And it works! (you should find that it generates the same codes as the Google Authenticator app)

Usage

  1. Add the required files to your project:
  2. OTPGenerator.m/.h*
  3. HOTPGenerator.m/.h*
  4. MF_Base32Additions.m/.h
Note: i and ii are not ARC-compliant so be sure to add the `-fno-objc-arc` Compiler Flag under your target's Build Phases > Compile Sources
  1. Imports:

    #import "TOTPGenerator.h"
    #import "MF_Base32Additions.h"
    
  2. Setup i) secret string, ii) date, and iii) TOTPGenerator object:

    NSString *secret = @"abcdefghijklmnop";
    NSData *secretData = [NSData dataWithBase32String:secret];
    NSDate *now = [NSDate date];
    TOTPGenerator *generator = [[TOTPGenerator alloc] initWithSecret:secretData 
    algorithm:kOTPGeneratorSHA1Algorithm digits:6 period:30];
    

    Note:

    • digits can be 6-8
    • period is the PIN expiry period in seconds
    • algorithm can be either kOTPGeneratorSHA1Algorithm (default), kOTPGeneratorSHA256Algorithm, kOTPGeneratorSHA512Algorithm, or kOTPGeneratorSHAMD5Algorithm.
  3. Generate PIN:

    NSString *pin = [generator generateOTPForDate:now];
    

Safe travels.

Credits

About

Simple, single-view iOS implementation of TOTP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published