Skip to content

Commit

Permalink
Fixed data types for 64-bit. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leah committed May 20, 2014
1 parent 77f194d commit 12165e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
12 changes: 6 additions & 6 deletions Zxcvbn/Zxcvbn/DBMatcher.h
Expand Up @@ -8,10 +8,10 @@

@interface DBMatcher : NSObject

@property (nonatomic, assign) int keyboardAverageDegree;
@property (nonatomic, assign) int keypadAverageDegree;
@property (nonatomic, assign) int keyboardStartingPositions;
@property (nonatomic, assign) int keypadStartingPositions;
@property (nonatomic, assign) NSUInteger keyboardAverageDegree;
@property (nonatomic, assign) NSUInteger keypadAverageDegree;
@property (nonatomic, assign) NSUInteger keyboardStartingPositions;
@property (nonatomic, assign) NSUInteger keypadStartingPositions;

- (NSArray *)omnimatch:(NSString *)password userInputs:(NSArray *)userInputs;

Expand All @@ -21,8 +21,8 @@

@property (nonatomic, assign) NSString *pattern;
@property (strong, nonatomic) NSString *token;
@property (nonatomic, assign) int i;
@property (nonatomic, assign) int j;
@property (nonatomic, assign) NSUInteger i;
@property (nonatomic, assign) NSUInteger j;
@property (nonatomic, assign) float entropy;
@property (nonatomic, assign) int cardinality;

Expand Down
16 changes: 8 additions & 8 deletions Zxcvbn/Zxcvbn/DBMatcher.m
Expand Up @@ -71,7 +71,7 @@ - (NSArray *)omnimatch:(NSString *)password userInputs:(NSArray *)userInputs
- (NSMutableArray *)dictionaryMatch:(NSString *)password rankedDict:(NSMutableDictionary *)rankedDict
{
NSMutableArray *result = [[NSMutableArray alloc] init];
int length = [password length];
NSUInteger length = [password length];
NSString *passwordLower = [password lowercaseString];

for (int i = 0; i < length; i++) {
Expand Down Expand Up @@ -474,13 +474,13 @@ - (MatcherBlock)sequenceMatch
int j = i + 1;
NSString *seq = nil; // either lower, upper, or digits
NSString *seqName = nil;
int seqDirection = 0; // 1 for ascending seq abcd, -1 for dcba
NSUInteger seqDirection = 0; // 1 for ascending seq abcd, -1 for dcba
for (NSString *seqCandidateName in sequences) {
NSString *seqCandidate = [sequences objectForKey:seqCandidateName];
int iN = [seqCandidate rangeOfString:[password substringWithRange:NSMakeRange(i, 1)]].location;
int jN = j < [password length] ? [seqCandidate rangeOfString:[password substringWithRange:NSMakeRange(j, 1)]].location : NSNotFound;
NSUInteger iN = [seqCandidate rangeOfString:[password substringWithRange:NSMakeRange(i, 1)]].location;
NSUInteger jN = j < [password length] ? [seqCandidate rangeOfString:[password substringWithRange:NSMakeRange(j, 1)]].location : NSNotFound;
if (iN != NSNotFound && jN != NSNotFound) {
int direction = jN - iN;
NSUInteger direction = jN - iN;
if (direction == 1 || direction == -1) {
seq = seqCandidate;
seqName = seqCandidateName;
Expand All @@ -493,8 +493,8 @@ - (MatcherBlock)sequenceMatch
while (YES) {
NSString *prevChar = [password substringWithRange:NSMakeRange(j - 1, 1)];
NSString *curChar = j < [password length] ? [password substringWithRange:NSMakeRange(j, 1)] : nil;
int prevN = [seq rangeOfString:prevChar].location;
int curN = curChar == nil ? NSNotFound : [seq rangeOfString:curChar].location;
NSUInteger prevN = [seq rangeOfString:prevChar].location;
NSUInteger curN = curChar == nil ? NSNotFound : [seq rangeOfString:curChar].location;
if (curN - prevN == seqDirection) {
j++;
} else {
Expand All @@ -505,7 +505,7 @@ - (MatcherBlock)sequenceMatch
match.j = j - 1;
match.token = [password substringWithRange:NSMakeRange(i, j - i)];
match.sequenceName = seqName;
match.sequenceSpace = [seq length];
match.sequenceSpace = (int)[seq length];
match.ascending = seqDirection == 1;
[result addObject:match];
}
Expand Down
30 changes: 15 additions & 15 deletions Zxcvbn/Zxcvbn/DBScorer.m
Expand Up @@ -30,13 +30,13 @@ - (DBResult *)minimumEntropyMatchSequence:(NSString *)password matches:(NSArray
[upToK insertObject:[NSNumber numberWithFloat:[get(upToK, k-1) floatValue] + lg(bruteforceCardinality)] atIndex:k];
[backpointers insertObject:[NSNull null] atIndex:k];
for (DBMatch *match in matches) {
int i = match.i;
int j = match.j;
NSUInteger i = match.i;
NSUInteger j = match.j;
if (j != k) {
continue;
}
// see if best entropy up to i-1 + entropy of this match is less than the current minimum at j.
float candidateEntropy = [get(upToK, i-1) floatValue] + [self calcEntropy:match];
float candidateEntropy = [get(upToK, (int)i-1) floatValue] + [self calcEntropy:match];
if (candidateEntropy < [[upToK objectAtIndex:j] floatValue]) {
[upToK insertObject:[NSNumber numberWithFloat:candidateEntropy] atIndex:j];
[backpointers insertObject:match atIndex:j];
Expand All @@ -46,7 +46,7 @@ - (DBResult *)minimumEntropyMatchSequence:(NSString *)password matches:(NSArray

// walk backwards and decode the best sequence
NSMutableArray *matchSequence = [[NSMutableArray alloc] init];
int k = [password length] - 1;
NSInteger k = [password length] - 1;
while (k >= 0) {
DBMatch *match = [backpointers objectAtIndex:k];
if (![match isEqual:[NSNull null]]) {
Expand All @@ -60,7 +60,7 @@ - (DBResult *)minimumEntropyMatchSequence:(NSString *)password matches:(NSArray

// fill in the blanks between pattern matches with bruteforce "matches"
// that way the match sequence fully covers the password: match1.j == match2.i - 1 for every adjacent match1, match2.
DBMatch* (^makeBruteforceMatch)(int i, int j) = ^ DBMatch* (int i, int j) {
DBMatch* (^makeBruteforceMatch)(NSUInteger i, NSUInteger j) = ^ DBMatch* (NSUInteger i, NSUInteger j) {
DBMatch *match = [[DBMatch alloc] init];
match.pattern = @"bruteforce";
match.i = i;
Expand All @@ -73,8 +73,8 @@ - (DBResult *)minimumEntropyMatchSequence:(NSString *)password matches:(NSArray
k = 0;
NSMutableArray *matchSequenceCopy = [[NSMutableArray alloc] init];
for (DBMatch *match in matchSequence) {
int i = match.i;
int j = match.j;
NSUInteger i = match.i;
NSUInteger j = match.j;
if (i - k > 0) {
[matchSequenceCopy addObject:makeBruteforceMatch(k, i-1)];
}
Expand Down Expand Up @@ -232,8 +232,8 @@ - (float)dateEntropy:(DBMatch *)match
- (float)spatialEntropy:(DBMatch *)match
{
DBMatcher *matcher = [[DBMatcher alloc] init];
int s;
int d;
NSUInteger s;
NSUInteger d;
if ([@[@"qwerty", @"dvorak"] containsObject:match.graph]) {
s = matcher.keyboardStartingPositions;
d = matcher.keyboardAverageDegree;
Expand All @@ -242,7 +242,7 @@ - (float)spatialEntropy:(DBMatch *)match
d = matcher.keypadAverageDegree;
}
int possibilities = 0;
int L = [match.token length];
NSUInteger L = [match.token length];
int t = match.turns;
// estimate the number of possible patterns w/ length L or less with t turns or less.
for (int i = 2; i <= L; i++) {
Expand All @@ -256,8 +256,8 @@ - (float)spatialEntropy:(DBMatch *)match
// math is similar to extra entropy from uppercase letters in dictionary matches.
if (match.shiftedCount) {
int S = match.shiftedCount;
int U = [match.token length] - match.shiftedCount; // unshifted count
int possibilities = 0;
NSUInteger U = [match.token length] - match.shiftedCount; // unshifted count
NSUInteger possibilities = 0;
for (int i = 0; i <= MIN(S, U); i++) {
possibilities += binom(S + U, i);
}
Expand Down Expand Up @@ -323,8 +323,8 @@ - (int)extraL33tEntropy:(DBMatch *)match

for (NSString *subbed in [match.sub allKeys]) {
NSString *unsubbed = [match.sub objectForKey:subbed];
int subLength = [[match.token componentsSeparatedByString:subbed] count] - 1;
int unsubLength = [[match.token componentsSeparatedByString:unsubbed] count] - 1;
NSUInteger subLength = [[match.token componentsSeparatedByString:subbed] count] - 1;
NSUInteger unsubLength = [[match.token componentsSeparatedByString:unsubbed] count] - 1;
for (int i = 0; i <= MIN(unsubLength, subLength); i++) {
possibilities += binom(unsubLength + subLength, i);
}
Expand Down Expand Up @@ -385,7 +385,7 @@ - (NSString *)displayTime:(int)seconds

#pragma mark - functions

float binom(int n, int k)
float binom(NSUInteger n, NSUInteger k)
{
// Returns binomial coefficient (n choose k).
// http://blog.plover.com/math/choose.html
Expand Down

0 comments on commit 12165e1

Please sign in to comment.