Merge pull request #363 from mariosangiorgio/master

Improved password generation UI as suggested in issue #355
This commit is contained in:
Michael Starke
2015-08-10 15:21:16 +02:00
3 changed files with 39 additions and 18 deletions

View File

@@ -148,20 +148,24 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) {
#pragma mark Actions #pragma mark Actions
- (IBAction)_generatePassword:(id)sender { - (IBAction)_generatePassword:(id)sender {
if(self.useCustomString) { self.password = [NSString passwordWithCharactersets:self.characterFlags
if([[self.customCharactersTextField stringValue] length] > 0) { withCustomCharacters:self._customCharacters
self.password = [self.customCharactersTextField.stringValue passwordWithLength:self.passwordLength]; length:self.passwordLength];
}
- (NSString*)_customCharacters{
if(self.useCustomString && [[self.customCharactersTextField stringValue] length] > 0) {
return self.customCharactersTextField.stringValue;
} }
} else{
else { return @"";
self.password = [NSString passwordWithCharactersets:self.characterFlags length:self.passwordLength]; }
}
} }
- (IBAction)_toggleCharacters:(id)sender { - (IBAction)_toggleCharacters:(id)sender {
self.setDefaultButton.enabled = YES; self.setDefaultButton.enabled = YES;
self.characterFlags ^= [sender tag]; self.characterFlags ^= [sender tag];
self.useCustomString = NO;
[self reset]; [self reset];
} }
@@ -321,15 +325,10 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) {
if(self.useCustomString) { if(self.useCustomString) {
self.customButton.state = NSOnState; self.customButton.state = NSOnState;
} }
self.customCharactersTextField.stringValue = self.customString;
self.customCharactersTextField.enabled = self.useCustomString; 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 */ /* Set to defaults, if we got nothing */
if(self.characterFlags == 0) { if(self.characterFlags == 0 && !self.useCustomString) {
self.characterFlags = MPPasswordCharactersAll; self.characterFlags = MPPasswordCharactersAll;
} }

View File

@@ -26,7 +26,9 @@ typedef NS_OPTIONS(NSUInteger, MPPasswordCharacterFlags) {
* *
* @return new password with only the allowed characters. * @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 * Creats a password based on the supplied string
* *

View File

@@ -32,6 +32,20 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) {
return characterString; 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) @implementation NSString (MPPasswordCreation)
+ (NSString *)passwordFromString:(NSString *)source length:(NSUInteger)length { + (NSString *)passwordFromString:(NSString *)source length:(NSUInteger)length {
@@ -42,9 +56,13 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) {
return password; return password;
} }
+ (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters length:(NSUInteger)length { + (NSString *)passwordWithCharactersets:(MPPasswordCharacterFlags)allowedCharacters
withCustomCharacters:(NSString*)customCharacters
length:(NSUInteger)length {
NSMutableString *password = [NSMutableString stringWithCapacity:length]; NSMutableString *password = [NSMutableString stringWithCapacity:length];
NSString *characters = allowedCharactersString(allowedCharacters); NSString *characters = mergeWithoutDuplicates(
allowedCharactersString(allowedCharacters),
customCharacters);
while([password length] < length) { while([password length] < length) {
NSString *randomCharacter = [characters randomCharacter]; NSString *randomCharacter = [characters randomCharacter];
if([randomCharacter length] > 0) { if([randomCharacter length] > 0) {
@@ -67,7 +85,9 @@ static NSString *allowedCharactersString(MPPasswordCharacterFlags flags) {
if(useCustomString && [customString length] > 0) { if(useCustomString && [customString length] > 0) {
return [customString passwordWithLength:passwordLength]; return [customString passwordWithLength:passwordLength];
} }
return [NSString passwordWithCharactersets:characterFlags length:passwordLength]; return [NSString passwordWithCharactersets:characterFlags
withCustomCharacters:@""
length:passwordLength];
} }
- (NSString *)passwordWithLength:(NSUInteger)length { - (NSString *)passwordWithLength:(NSUInteger)length {