mirror of
https://github.com/MacPass/MacPass.git
synced 2026-02-01 10:08:25 +00:00
Updated entropy calculation to account for required characters
This commit is contained in:
@@ -175,7 +175,7 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) {
|
|||||||
length:self.passwordLength];
|
length:self.passwordLength];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString*)_customCharacters{
|
- (NSString *)_customCharacters{
|
||||||
if(self.useCustomString && self.customCharactersTextField.stringValue.length > 0) {
|
if(self.useCustomString && self.customCharactersTextField.stringValue.length > 0) {
|
||||||
return self.customCharactersTextField.stringValue;
|
return self.customCharactersTextField.stringValue;
|
||||||
}
|
}
|
||||||
@@ -319,6 +319,7 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) {
|
|||||||
- (void)controlTextDidChange:(NSNotification *)obj {
|
- (void)controlTextDidChange:(NSNotification *)obj {
|
||||||
if([obj object] == self.customCharactersTextField) {
|
if([obj object] == self.customCharactersTextField) {
|
||||||
self.setDefaultButton.enabled = YES;
|
self.setDefaultButton.enabled = YES;
|
||||||
|
[self _resetCharacters];
|
||||||
[self _generatePassword:nil];
|
[self _generatePassword:nil];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -394,7 +395,10 @@ typedef NS_ENUM(NSUInteger, MPPasswordRating) {
|
|||||||
|
|
||||||
// ensure minimum character lenght
|
// ensure minimum character lenght
|
||||||
if(self.ensureOccurance) {
|
if(self.ensureOccurance) {
|
||||||
|
NSUInteger minimumLength = [NSString minimumPasswordLengthWithCharacterSet:self.characterFlags customCharacters:[self _customCharacters] ensureOccurance:self.ensureOccurance];
|
||||||
|
if(self.passwordLength < minimumLength) {
|
||||||
|
self.passwordLength = minimumLength;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,16 +160,39 @@ static NSString *mergeWithoutDuplicates(NSString* baseCharacters, NSString* cust
|
|||||||
|
|
||||||
|
|
||||||
- (CGFloat)entropyWhithCharacterSet:(MPPasswordCharacterFlags)characterSet customCharacters:(NSString *)customCharacters ensureOccurance:(BOOL)ensureOccurance {
|
- (CGFloat)entropyWhithCharacterSet:(MPPasswordCharacterFlags)characterSet customCharacters:(NSString *)customCharacters ensureOccurance:(BOOL)ensureOccurance {
|
||||||
|
CGFloat passwordLength = self.composedCharacterLength;
|
||||||
|
CGFloat entropy = 0;
|
||||||
if(ensureOccurance) {
|
if(ensureOccurance) {
|
||||||
return 0;
|
CGLError alphabetCount = 0;
|
||||||
}
|
if(characterSet & MPPasswordCharactersLowerCase) {
|
||||||
else {
|
alphabetCount = (CGFloat)characterClassMap()[@(MPPasswordCharactersLowerCase)].length;
|
||||||
NSString *characters = nil;
|
entropy += log2(alphabetCount);
|
||||||
characters = mergeWithoutDuplicates(allowedCharactersString(characterSet), customCharacters);
|
}
|
||||||
CGFloat alphabetCount = characters.composedCharacterLength;
|
if(characterSet & MPPasswordCharactersUpperCase) {
|
||||||
CGFloat passwordLength = self.composedCharacterLength;
|
alphabetCount = (CGFloat)characterClassMap()[@(MPPasswordCharactersUpperCase)].length;
|
||||||
return passwordLength * ( log10(alphabetCount) / log10(2) );
|
entropy += log2(alphabetCount);
|
||||||
|
}
|
||||||
|
if(characterSet & MPPasswordCharactersNumbers) {
|
||||||
|
alphabetCount = (CGFloat)characterClassMap()[@(MPPasswordCharactersNumbers)].length;
|
||||||
|
entropy += log2(alphabetCount);
|
||||||
|
|
||||||
|
}
|
||||||
|
if(characterSet & MPPasswordCharactersSymbols){
|
||||||
|
alphabetCount = (CGFloat)characterClassMap()[@(MPPasswordCharactersSymbols)].length;
|
||||||
|
entropy += log2(alphabetCount);
|
||||||
|
|
||||||
|
}
|
||||||
|
if(customCharacters.composedCharacterLength > 0) {
|
||||||
|
entropy += log2(customCharacters.composedCharacterLength);
|
||||||
|
}
|
||||||
|
NSUInteger minLenght = [NSString minimumPasswordLengthWithCharacterSet:characterSet customCharacters:customCharacters ensureOccurance:ensureOccurance];
|
||||||
|
passwordLength -= minLenght;
|
||||||
}
|
}
|
||||||
|
NSString *characters = mergeWithoutDuplicates(allowedCharactersString(characterSet), customCharacters);
|
||||||
|
CGFloat alphabetCount = characters.composedCharacterLength;
|
||||||
|
entropy += passwordLength * log2(alphabetCount);
|
||||||
|
|
||||||
|
return entropy;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)shuffledString {
|
- (NSString *)shuffledString {
|
||||||
|
|||||||
Reference in New Issue
Block a user