mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 18:42:24 +00:00
Merge pull request #363 from mariosangiorgio/master
Improved password generation UI as suggested in issue #355
This commit is contained in:
@@ -148,20 +148,24 @@ 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{
|
||||
return @"";
|
||||
}
|
||||
else {
|
||||
self.password = [NSString passwordWithCharactersets:self.characterFlags length:self.passwordLength];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (IBAction)_toggleCharacters:(id)sender {
|
||||
self.setDefaultButton.enabled = YES;
|
||||
self.characterFlags ^= [sender tag];
|
||||
self.useCustomString = NO;
|
||||
[self reset];
|
||||
}
|
||||
|
||||
@@ -321,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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 {
|
||||
@@ -42,9 +56,13 @@ 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);
|
||||
NSString *characters = mergeWithoutDuplicates(
|
||||
allowedCharactersString(allowedCharacters),
|
||||
customCharacters);
|
||||
while([password length] < length) {
|
||||
NSString *randomCharacter = [characters randomCharacter];
|
||||
if([randomCharacter length] > 0) {
|
||||
@@ -67,7 +85,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 {
|
||||
|
||||
Reference in New Issue
Block a user