From e4332530eb7dc446136d71203472a95c471d988f Mon Sep 17 00:00:00 2001 From: Mario Sangiorgio Date: Sat, 8 Aug 2015 00:40:19 +0100 Subject: [PATCH 1/3] Added support for both standard and custom characters sets --- MacPass/MPPasswordCreatorViewController.m | 19 ++++++++++++------- MacPass/NSString+MPPasswordCreation.h | 4 +++- MacPass/NSString+MPPasswordCreation.m | 8 ++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/MacPass/MPPasswordCreatorViewController.m b/MacPass/MPPasswordCreatorViewController.m index ecdfad07..5d83dff1 100644 --- a/MacPass/MPPasswordCreatorViewController.m +++ b/MacPass/MPPasswordCreatorViewController.m @@ -148,14 +148,19 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) { #pragma mark Actions - (IBAction)_generatePassword:(id)sender { - if(self.useCustomString) { - if([[self.customCharactersTextField stringValue] length] > 0) { - self.password = [self.customCharactersTextField.stringValue passwordWithLength:self.passwordLength]; + self.password = [NSString passwordWithCharactersets:self.characterFlags + withCustomCharacters:self._customCharacters + length:self.passwordLength]; +} + +- (NSString*)_customCharacters{ + if(self.useCustomString && [[self.customCharactersTextField stringValue] length] > 0) { + return self.customCharactersTextField.stringValue; } - } - else { - self.password = [NSString passwordWithCharactersets:self.characterFlags length:self.passwordLength]; - } + else{ + return @""; + } + } - (IBAction)_toggleCharacters:(id)sender { diff --git a/MacPass/NSString+MPPasswordCreation.h b/MacPass/NSString+MPPasswordCreation.h index 1e40094b..518c21b5 100644 --- a/MacPass/NSString+MPPasswordCreation.h +++ b/MacPass/NSString+MPPasswordCreation.h @@ -26,7 +26,9 @@ typedef NS_OPTIONS(NSUInteger, MPPasswordCharacterFlags) { * * @return new password with only the allowed characters. */ -+ (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters length:(NSUInteger)theLength; ++ (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters + withCustomCharacters:(NSString*)customCharacters + length:(NSUInteger)theLength; /** * Creats a password based on the supplied string * diff --git a/MacPass/NSString+MPPasswordCreation.m b/MacPass/NSString+MPPasswordCreation.m index bb051772..3b9fa1b4 100644 --- a/MacPass/NSString+MPPasswordCreation.m +++ b/MacPass/NSString+MPPasswordCreation.m @@ -42,7 +42,9 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) { return password; } -+ (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters length:(NSUInteger)length { ++ (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters + withCustomCharacters:(NSString*)customCharacters + length:(NSUInteger)length { NSMutableString *password = [NSMutableString stringWithCapacity:length]; NSString *characters = allowedCharactersString(allowedCharacters); while([password length] < length) { @@ -67,7 +69,9 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) { if(useCustomString && [customString length] > 0) { return [customString passwordWithLength:passwordLength]; } - return [NSString passwordWithCharactersets:characterFlags length:passwordLength]; + return [NSString passwordWithCharactersets:characterFlags + withCustomCharacters:@"" + length:passwordLength]; } - (NSString *)passwordWithLength:(NSUInteger)length { From a1297b1409d96c1a669aacb91395aa02d6c2e99e Mon Sep 17 00:00:00 2001 From: Mario Sangiorgio Date: Sat, 8 Aug 2015 01:01:21 +0100 Subject: [PATCH 2/3] Merging standard and custom characters sets --- MacPass/NSString+MPPasswordCreation.m | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/MacPass/NSString+MPPasswordCreation.m b/MacPass/NSString+MPPasswordCreation.m index 3b9fa1b4..49c6f13e 100644 --- a/MacPass/NSString+MPPasswordCreation.m +++ b/MacPass/NSString+MPPasswordCreation.m @@ -32,6 +32,20 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) { return characterString; } +static NSString *mergeWithoutDuplicates(NSString* baseCharacters, NSString* customCharacters){ + NSInteger maxLength =[baseCharacters length] + [customCharacters length]; + NSMutableString* mergedCharacters = [NSMutableString stringWithCapacity: maxLength]; + [mergedCharacters appendString:baseCharacters]; + [customCharacters enumerateSubstringsInRange: NSMakeRange(0, [customCharacters length]) + options: NSStringEnumerationByComposedCharacterSequences + usingBlock: ^(NSString *inSubstring, NSRange inSubstringRange, NSRange inEnclosingRange, BOOL *outStop) { + if(![mergedCharacters containsString:inSubstring]){ + [mergedCharacters appendString:inSubstring]; + } + }]; + return [NSString stringWithString:mergedCharacters]; +} + @implementation NSString (MPPasswordCreation) + (NSString *)passwordFromString:(NSString *)source length:(NSUInteger)length { @@ -46,7 +60,9 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) { withCustomCharacters:(NSString*)customCharacters length:(NSUInteger)length { NSMutableString *password = [NSMutableString stringWithCapacity:length]; - NSString *characters = allowedCharactersString(allowedCharacters); + NSString *characters = mergeWithoutDuplicates( + allowedCharactersString(allowedCharacters), + customCharacters); while([password length] < length) { NSString *randomCharacter = [characters randomCharacter]; if([randomCharacter length] > 0) { From 85010d66a32b6a15c4a1aeb1746a9a77de114f43 Mon Sep 17 00:00:00 2001 From: Mario Sangiorgio Date: Sat, 8 Aug 2015 01:14:48 +0100 Subject: [PATCH 3/3] UI buttons binding --- MacPass/MPPasswordCreatorViewController.m | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/MacPass/MPPasswordCreatorViewController.m b/MacPass/MPPasswordCreatorViewController.m index 5d83dff1..f9a47784 100644 --- a/MacPass/MPPasswordCreatorViewController.m +++ b/MacPass/MPPasswordCreatorViewController.m @@ -166,7 +166,6 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) { - (IBAction)_toggleCharacters:(id)sender { self.setDefaultButton.enabled = YES; self.characterFlags ^= [sender tag]; - self.useCustomString = NO; [self reset]; } @@ -326,15 +325,10 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) { if(self.useCustomString) { self.customButton.state = NSOnState; } - self.customCharactersTextField.stringValue = self.customString; self.customCharactersTextField.enabled = self.useCustomString; - self.upperCaseButton.enabled = !self.useCustomString; - self.lowerCaseButton.enabled = !self.useCustomString; - self.numbersButton.enabled = !self.useCustomString; - self.symbolsButton.enabled = !self.useCustomString; /* Set to defaults, if we got nothing */ - if(self.characterFlags == 0) { + if(self.characterFlags == 0 && !self.useCustomString) { self.characterFlags = MPPasswordCharactersAll; }