Permalink
Browse files

Fixing incorrect PIN computation on platforms where

sizeof(unsigned long) == 8.

This is applying the patch referenced in the original issue here:
https://code.google.com/p/google-authenticator/issues/detail?id=374

Also fixing a compilation warning in string format specifier.
  • Loading branch information...
mrollins committed Dec 9, 2014
1 parent b33edf4 commit 4ccd851af1a42f8ec6fa95f4b7104d8464616d40
Showing with 4 additions and 4 deletions.
  1. +4 −4 mobile/ios/Classes/OTPGenerator.m
@@ -131,9 +131,9 @@ - (NSString *)generateOTPForCounter:(uint64_t)counter {

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_];
uint32_t truncatedHash =
NSSwapBigIntToHost(*((uint32_t *)&ptr[offset])) & 0x7fffffff;
uint32_t pinValue = truncatedHash % kPinModTable[digits_];

_GTMDevLog(@"secret: %@", secret_);
_GTMDevLog(@"counter: %llu", counter);
@@ -142,7 +142,7 @@ - (NSString *)generateOTPForCounter:(uint64_t)counter {
_GTMDevLog(@"truncatedHash: %d", truncatedHash);
_GTMDevLog(@"pinValue: %d", pinValue);

return [NSString stringWithFormat:@"%0*d", digits_, pinValue];
return [NSString stringWithFormat:@"%0*u", (int)digits_, pinValue];
}

@end

0 comments on commit 4ccd851

Please sign in to comment.