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)
- Add the required files to your project:
- OTPGenerator.m/.h*
- HOTPGenerator.m/.h*
- 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
-
Imports:
#import "TOTPGenerator.h" #import "MF_Base32Additions.h"
-
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-8period
is the PIN expiry period in secondsalgorithm
can be eitherkOTPGeneratorSHA1Algorithm
(default),kOTPGeneratorSHA256Algorithm
,kOTPGeneratorSHA512Algorithm
, orkOTPGeneratorSHAMD5Algorithm
.
-
Generate PIN:
NSString *pin = [generator generateOTPForDate:now];
Safe travels.