no message
This commit is contained in:
1 parent
3165e2eef8
commit
2deafc8b58
21 files changed
+1783
-1151
No files matched your search
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2016 Giuseppe Nucifora <me@giuseppenucifora.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
Generated
Executable
+27
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// StrongestPasswordValidator.h
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 03/01/13.
|
||||
// Copyright (c) 2013 Giuseppe Nucifora. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface StrongestPasswordValidator : NSObject
|
||||
|
||||
typedef NS_ENUM(NSInteger, PasswordStrengthType) {
|
||||
PasswordStrengthTypeWeak,
|
||||
PasswordStrengthTypeModerate,
|
||||
PasswordStrengthTypeStrong
|
||||
};
|
||||
|
||||
+ (instancetype _Nonnull) sharedInstance;
|
||||
|
||||
- (void) setColor:(UIColor * _Nonnull) color forPasswordStrenghtType:(PasswordStrengthType) strenghtType;
|
||||
|
||||
- (void)checkPasswordStrength:(NSString * _Nonnull )password withBlock:(nullable void (^)(UIColor * _Nonnull color, PasswordStrengthType strenghtType)) responseBlock;
|
||||
|
||||
- (PasswordStrengthType)checkPasswordStrength:(NSString * _Nonnull)password;
|
||||
|
||||
@end
|
||||
Generated
Executable
+171
@@ -0,0 +1,171 @@
|
||||
|
||||
#import "StrongestPasswordValidator.h"
|
||||
|
||||
#define REGEX_PASSWORD_ONE_UPPERCASE @"^(?=.*[A-Z]).*$" //Should contains one or more uppercase letters
|
||||
#define REGEX_PASSWORD_ONE_LOWERCASE @"^(?=.*[a-z]).*$" //Should contains one or more lowercase letters
|
||||
#define REGEX_PASSWORD_ONE_NUMBER @"^(?=.*[0-9]).*$" //Should contains one or more number
|
||||
#define REGEX_PASSWORD_ONE_SYMBOL @"^(?=.*[!@#$%&_]).*$" //Should contains one or more symbol
|
||||
|
||||
@interface StrongestPasswordValidator()
|
||||
|
||||
@property (nonatomic, strong) UIColor *weakColor;
|
||||
@property (nonatomic, strong) UIColor *moderateColor;
|
||||
@property (nonatomic, strong) UIColor *strongColor;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation StrongestPasswordValidator
|
||||
|
||||
static StrongestPasswordValidator *SINGLETON = nil;
|
||||
|
||||
static bool isFirstAccess = YES;
|
||||
|
||||
+ (instancetype)sharedInstance
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
- (void) setColor:(UIColor *)color forPasswordStrenghtType:(PasswordStrengthType)strenghtType {
|
||||
switch (strenghtType) {
|
||||
case PasswordStrengthTypeWeak: {
|
||||
_weakColor = color;
|
||||
break;
|
||||
}
|
||||
case PasswordStrengthTypeModerate: {
|
||||
_moderateColor = color;
|
||||
break;
|
||||
}
|
||||
case PasswordStrengthTypeStrong: {
|
||||
_strongColor = color;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Life Cycle
|
||||
|
||||
+ (instancetype) allocWithZone:(NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (instancetype)copyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (instancetype)mutableCopyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
- (instancetype)copy
|
||||
{
|
||||
return [[StrongestPasswordValidator alloc] init];
|
||||
}
|
||||
|
||||
- (instancetype)mutableCopy
|
||||
{
|
||||
return [[StrongestPasswordValidator alloc] init];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_weakColor = [UIColor redColor];
|
||||
_moderateColor = [UIColor yellowColor];
|
||||
_strongColor = [UIColor greenColor];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)checkPasswordStrength:(NSString * _Nonnull )password withBlock:(nullable void (^)(UIColor * _Nonnull color, PasswordStrengthType strenghtType)) responseBlock; {
|
||||
|
||||
if (responseBlock) {
|
||||
|
||||
PasswordStrengthType strenght = [self checkPasswordStrength:password];
|
||||
|
||||
switch (strenght) {
|
||||
case PasswordStrengthTypeWeak: {
|
||||
responseBlock(_weakColor,strenght);
|
||||
break;
|
||||
}
|
||||
case PasswordStrengthTypeModerate: {
|
||||
responseBlock(_moderateColor,strenght);
|
||||
break;
|
||||
}
|
||||
case PasswordStrengthTypeStrong: {
|
||||
responseBlock(_strongColor,strenght);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (PasswordStrengthType)checkPasswordStrength:(NSString *)password {
|
||||
NSInteger len = (long)password.length;
|
||||
//will contains password strength
|
||||
int strength = 0;
|
||||
|
||||
if (len == 0) {
|
||||
return PasswordStrengthTypeWeak;
|
||||
} else if (len <= 5) {
|
||||
strength++;
|
||||
} else if (len <= 10) {
|
||||
strength += 2;
|
||||
} else{
|
||||
strength += 3;
|
||||
}
|
||||
|
||||
strength += [self validateString:password withPattern:REGEX_PASSWORD_ONE_UPPERCASE caseSensitive:YES];
|
||||
strength += [self validateString:password withPattern:REGEX_PASSWORD_ONE_LOWERCASE caseSensitive:YES];
|
||||
strength += [self validateString:password withPattern:REGEX_PASSWORD_ONE_NUMBER caseSensitive:YES];
|
||||
strength += [self validateString:password withPattern:REGEX_PASSWORD_ONE_SYMBOL caseSensitive:YES];
|
||||
|
||||
if(strength <= 3){
|
||||
return PasswordStrengthTypeWeak;
|
||||
}else if(3 < strength && strength < 6){
|
||||
return PasswordStrengthTypeModerate;
|
||||
}else{
|
||||
return PasswordStrengthTypeStrong;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the input string with the given pattern and
|
||||
// return the result as a boolean
|
||||
- (int)validateString:(NSString *)string withPattern:(NSString *)pattern caseSensitive:(BOOL)caseSensitive
|
||||
{
|
||||
NSError *error = nil;
|
||||
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:((caseSensitive) ? 0 : NSRegularExpressionCaseInsensitive) error:&error];
|
||||
|
||||
NSAssert(regex, @"Unable to create regular expression");
|
||||
|
||||
NSRange textRange = NSMakeRange(0, string.length);
|
||||
NSRange matchRange = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];
|
||||
|
||||
BOOL didValidate = 0;
|
||||
|
||||
// Did we find a matching range
|
||||
if (matchRange.location != NSNotFound)
|
||||
didValidate = 1;
|
||||
|
||||
return didValidate;
|
||||
}
|
||||
|
||||
@end
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# StrongestPasswordValidator
|
||||
|
||||
[](https://travis-ci.org/Giuseppe Nucifora/StrongestPasswordValidator)
|
||||
[](http://cocoapods.org/pods/StrongestPasswordValidator)
|
||||
[](http://cocoapods.org/pods/StrongestPasswordValidator)
|
||||
[](http://cocoapods.org/pods/StrongestPasswordValidator)
|
||||
|
||||
## Usage
|
||||
|
||||
To run the example project, clone the repo, and run `pod install` from the Example directory first.
|
||||
|
||||
## Requirements
|
||||
|
||||
## Installation
|
||||
|
||||
StrongestPasswordValidator is available through [CocoaPods](http://cocoapods.org). To install
|
||||
it, simply add the following line to your Podfile:
|
||||
|
||||
```ruby
|
||||
pod "StrongestPasswordValidator"
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
Giuseppe Nucifora, me@giuseppenucifora.com
|
||||
|
||||
## License
|
||||
|
||||
StrongestPasswordValidator is available under the MIT license. See the LICENSE file for more info.
|
||||
Reference in new issue
Block a user