密码强度判断

作者: 太郎君 | 来源:发表于2016-07-18 20:31 被阅读281次

    提示弱密码无效

    1. 密码长度:12345
    2. 密码覆盖字符集:12345678
    3. 密码规则性: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

    相关文章

      网友评论

        本文标题:密码强度判断

        本文链接:https://www.haomeiwen.com/subject/sbgpjttx.html