Google Authenticator For Objective C

做~自己de王妃 提交于 2019-12-11 05:09:36

问题


I am developing an app that used Google Authenticator. For this scenario, The web application generate QRCode via Google Authenticator. And than i read this QRCode to generate OTP via Google Authentication in mobile app. I use below code to create OTP. When i check this created OTP is always wrong. I use Google Authenticator mobile app to check my OTP is correct or wrong.

Any one can help me?

- (NSString *)generateOTPWithCounter:(uint64_t)counter {
CCHmacAlgorithm alg;
NSUInteger hashLength = 0;
alg = kCCHmacAlgSHA256;
hashLength = CC_SHA256_DIGEST_LENGTH;

NSMutableData *hash = [NSMutableData dataWithLength:hashLength];

counter = NSSwapHostLongLongToBig(counter);
NSData *counterData = [NSData dataWithBytes:&counter
                                     length:sizeof(counter)];
CCHmacContext ctx;
CCHmacInit(&ctx, alg, [self.secret bytes], [self.secret length]);
CCHmacUpdate(&ctx, [counterData bytes], [counterData length]);
CCHmacFinal(&ctx, [hash mutableBytes]);

const char *ptr = [hash bytes];
unsigned char offset = ptr[hashLength-1] & 0x0f;
uint32_t truncatedHash =
NSSwapBigIntToHost(*((uint32_t *)&ptr[offset])) & 0x7fffffff;
uint32_t pinValue = truncatedHash % kPinModTable[8];

return [NSString stringWithFormat:@"%0*u", 8, pinValue];

}

EDIT : i figure out this problem. Now it's working properly. i use below code for NSData

-(NSData *)base32Decode:(NSString *)string {
GTMStringEncoding *coder =
[GTMStringEncoding stringEncodingWithString:kBase32Charset];
[coder addDecodeSynonyms:kBase32Synonyms];
[coder ignoreCharacters:kBase32Sep];
return [coder decode:string error:nil];

}

来源:https://stackoverflow.com/questions/43318487/google-authenticator-for-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!