Password Validator & Strength Evaluator
Navajo is named in honor of the famed code talkers of the Second World War.
Navajo by Mattt Thompson
This project is not 100% compatible with the original project.
github "jasonnam/Navajo-Swift"
use_frameworks!
pod 'Navajo-Swift'
import Navajo_Swift
Just copy the files in Source folder into your project.
Password strength is evaluated in terms of information entropy.
@IBOutlet private weak var passwordField: UITextField! = nil
@IBOutlet private weak var strengthLabel: UILabel! = nil
let password = passwordField.text ?? ""
let strength = Navajo.strength(of: password)
strengthLabel.text = Navajo.localizedString(for: strength)
var lengthRule = NJOLengthRule(min: 6, max: 24)
var uppercaseRule = NJORequiredCharacterRule(preset: .LowercaseCharacter)
validator = NJOPasswordValidator(rules: [lengthRule, uppercaseRule])
if let failingRules = validator.validate(password) {
var errorMessages: [String] = []
failingRules.forEach { rule in
errorMessages.append(rule.localizedErrorDescription)
}
validationLabel.textColor = UIColor.red
validationLabel.text = errorMessages.joined(separator: "\n")
} else {
validationLabel.textColor = UIColor.green
validationLabel.text = "Valid"
}
- Allowed Characters
- Required Characters (custom, lowercase, uppercase, decimal, symbol)
- Non-Dictionary Word
- Minimum / Maximum Length
- Predicate Match
- Regular Expression Match
- Block Evaluation
If you are using the Predicate and Regex rules, remember that when password is matching to them it is considered to be invalid. For example, we can check if users are using for example "password123" as their password by following rule object.
var rule = NJOPredicateRule(predicate: NSPredicate(format: "SELF BEGINSWITH %@", "PASSWORD"))
For the block rule, it is considered to be invalid when the block returns true.
Keys for the localizable strings Localization Tutorial or check the demo app in the repository.
- NAVAJO_VERY_WEAK
- NAVAJO_WEAK
- NAVAJO_REASONABLE
- NAVAJO_STRONG
- NAVAJO_VERY_STRONG
-
NAVAJO_ALLOWED_CHARACTER_ERROR
-
NAVAJO_REQUIRED_CHARACTER_REQUIRED_ERROR
-
NAVAJO_REQUIRED_CHARACTER_LOWERCASE_ERROR
-
NAVAJO_REQUIRED_CHARACTER_UPPERCASE_ERROR
-
NAVAJO_REQUIRED_CHARACTER_DECIMAL_DIGIT_ERROR
-
NAVAJO_REQUIRED_CHARACTER_SYMBOL_ERROR
-
NAVAJO_DICTIONARYWORD_ERROR
-
NAVAJO_LENGTH_ERROR
-
NAVAJO_PREDICATE_ERROR
-
NAVAJO_REGEX_ERROR
-
NAVAJO_BLOCK_ERROR
- Improved documentation
- Swift Package Manager Support
- Considering support for Swift throws - catch
Any feedback and pull requests are welcome :)
Navajo-Swift is available under the MIT license. See the LICENSE file for more info.