This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOTPGenerator.m
138 lines (116 loc) · 3.78 KB
/
OTPGenerator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// HOTPGenerator.m
//
// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
#import "OTPGenerator.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>
static NSUInteger kPinModTable[] = {
0,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
};
NSString *const kOTPGeneratorSHA1Algorithm = @"SHA1";
NSString *const kOTPGeneratorSHA256Algorithm = @"SHA256";
NSString *const kOTPGeneratorSHA512Algorithm = @"SHA512";
NSString *const kOTPGeneratorSHAMD5Algorithm = @"MD5";
@interface OTPGenerator ()
@property (readwrite, nonatomic, copy) NSString *algorithm;
@property (readwrite, nonatomic, copy) NSData *secret;
@end
@implementation OTPGenerator
+ (NSString *)defaultAlgorithm {
return kOTPGeneratorSHA1Algorithm;
}
+ (NSUInteger)defaultDigits {
return 6;
}
@synthesize algorithm = algorithm_;
@synthesize secret = secret_;
@synthesize digits = digits_;
- (id)init {
[self doesNotRecognizeSelector:_cmd];
return nil;
}
- (id)initWithSecret:(NSData *)secret
algorithm:(NSString *)algorithm
digits:(NSUInteger)digits {
if ((self = [super init])) {
algorithm_ = [algorithm copy];
secret_ = [secret copy];
digits_ = digits;
BOOL goodAlgorithm
= ([algorithm isEqualToString:kOTPGeneratorSHA1Algorithm] ||
[algorithm isEqualToString:kOTPGeneratorSHA256Algorithm] ||
[algorithm isEqualToString:kOTPGeneratorSHA512Algorithm] ||
[algorithm isEqualToString:kOTPGeneratorSHAMD5Algorithm]);
if (!goodAlgorithm || digits_ > 8 || digits_ < 6 || !secret_) {
[self release];
self = nil;
}
}
return self;
}
- (void)dealloc {
self.algorithm = nil;
self.secret = nil;
[super dealloc];
}
// Must be overriden by subclass.
- (NSString *)generateOTP {
[self doesNotRecognizeSelector:_cmd];
return nil;
}
- (NSString *)generateOTPForCounter:(uint64_t)counter {
CCHmacAlgorithm alg;
NSUInteger hashLength = 0;
if ([algorithm_ isEqualToString:kOTPGeneratorSHA1Algorithm]) {
alg = kCCHmacAlgSHA1;
hashLength = CC_SHA1_DIGEST_LENGTH;
} else if ([algorithm_ isEqualToString:kOTPGeneratorSHA256Algorithm]) {
alg = kCCHmacAlgSHA256;
hashLength = CC_SHA256_DIGEST_LENGTH;
} else if ([algorithm_ isEqualToString:kOTPGeneratorSHA512Algorithm]) {
alg = kCCHmacAlgSHA512;
hashLength = CC_SHA512_DIGEST_LENGTH;
} else if ([algorithm_ isEqualToString:kOTPGeneratorSHAMD5Algorithm]) {
alg = kCCHmacAlgMD5;
hashLength = CC_MD5_DIGEST_LENGTH;
} else {
return nil;
}
NSMutableData *hash = [NSMutableData dataWithLength:hashLength];
counter = NSSwapHostLongLongToBig(counter);
NSData *counterData = [NSData dataWithBytes:&counter
length:sizeof(counter)];
CCHmacContext ctx;
CCHmacInit(&ctx, alg, [secret_ bytes], [secret_ length]);
CCHmacUpdate(&ctx, [counterData bytes], [counterData length]);
CCHmacFinal(&ctx, [hash mutableBytes]);
const char *ptr = [hash bytes];
unsigned char offset = ptr[hashLength-1] & 0x0f;
unsigned long truncatedHash =
NSSwapBigLongToHost(*((unsigned long *)&ptr[offset])) & 0x7fffffff;
unsigned long pinValue = truncatedHash % kPinModTable[digits_];
return [NSString stringWithFormat:@"%0*lu", digits_, pinValue];
}
@end