Use arc4random_uniform() to avoid modulo bias. (#669)

Avoid a possible modulo bias in randomCharacter by using arc4random_uniform().
This commit is contained in:
Ryan Rogers
2017-10-24 10:01:38 -07:00
committed by Michael Starke
parent 0161435418
commit 73149dd125

View File

@@ -112,10 +112,7 @@ static NSString *mergeWithoutDuplicates(NSString* baseCharacters, NSString* cust
if(self.length == 0) {
return nil;
}
NSData *data = [NSData kpk_dataWithRandomBytes:sizeof(NSUInteger)];
NSUInteger randomIndex;
[data getBytes:&randomIndex length:data.length];
return [self composedCharacterAtIndex:(randomIndex % self.composedCharacterLength)];
return [self composedCharacterAtIndex:arc4random_uniform((int)[self length])];
}
- (CGFloat)entropyWhithPossibleCharacterSet:(MPPasswordCharacterFlags)allowedCharacters orCustomCharacters:(NSString *)customCharacters {