提示弱密码无效
- 密码长度:12345
- 密码覆盖字符集:12345678
- 密码规则性:1234abcd
typedef NS_ENUM(UInt32, PasswordStrengthType) {
PasswordStrengthTypeTooShort,
PasswordStrengthTypeTooLong,
PasswordStrengthTypeSingleSet,
PasswordStrengthTypeMatchList,
PasswordStrengthTypeValid,
};
@interface PasswordStrengthEvaluator : NSObject
+ (PasswordStrengthEvaluator *)sharedInstance;
- (PasswordStrengthType)checkPasswordStrength:(NSString *)password;
@end
NSString * const kRegexPasswordHasUppercase = @"^(?=.*[A-Z]).*$";
NSString * const kRegexPasswordHasLowercase = @"^(?=.*[a-z]).*$";
NSString * const kRegexPasswordHasNumber = @"^(?=.*[0-9]).*$";
NSString * const kRegexPasswordHasSymbol = @"^(?=.*[!@#$%&_]).*$";
#import "PasswordStrengthEvaluator.h"
@implementation PasswordStrengthEvaluator {
NSArray *_weakPwdList;
}
+ (PasswordStrengthEvaluator *)sharedInstance
{
static dispatch_once_t onceToken;
static PasswordStrengthEvaluator *sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[PasswordStrengthEvaluator alloc] init];
});
return sharedInstance;
}
- (PasswordStrengthType)checkPasswordStrength:(NSString *)password
{
if (password.length < 8) return PasswordStrengthTypeTooShort;
if (password.length > 16) return PasswordStrengthTypeTooLong;
BOOL hasUppercase = [self isPattern:kRegexPasswordHasUppercase validateString:password caseSensitive:YES];
BOOL hasLowercase = [self isPattern:kRegexPasswordHasLowercase validateString:password caseSensitive:YES];
BOOL hasNumber = [self isPattern:kRegexPasswordHasNumber validateString:password caseSensitive:YES];
BOOL hasSymbol = [self isPattern:kRegexPasswordHasSymbol validateString:password caseSensitive:YES];
if (hasUppercase + hasLowercase + hasNumber + hasSymbol < 2) return PasswordStrengthTypeSingleSet;
for (NSString *weakpwd in _weakPwdList) {
if ([weakpwd isEqualToString:password]) {
return PasswordStrengthTypeMatchList;
};
}
return PasswordStrengthTypeValid;
}
- (BOOL)isPattern:(NSString *)pattern validateString:(NSString *)string caseSensitive:(BOOL)caseSensitive
{
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:((caseSensitive) ? 0 : NSRegularExpressionCaseInsensitive) error:&error];
NSRange textRange = NSMakeRange(0, string.length);
NSRange matchRange = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];
return matchRange.location != NSNotFound;
}
@end
提示密码强弱等级
Paste_Image.png
网友评论