sxua / Navajo-Swift

Password Validator & Strength Evaluator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Navajo-Swift

Password Validator & Strength Evaluator

Navajo is named in honor of the famed code talkers of the Second World War.

Original Project

Navajo by Mattt Thompson

This project is not 100% compatible with the original project.

Installation

License Swift 2.0 Platform iOS Travis-CI Carthage compatible Version

Carthage

github "jasonnam/Navajo-Swift"

CocoaPods

use_frameworks!
pod 'Navajo-Swift'
import Navajo_Swift

Manual

Just copy the files in Source folder into your project.

Usage

Evaluating Password Strength

Password strength is evaluated in terms of information entropy.

@IBOutlet weak var passwordField: UITextField! = nil
@IBOutlet weak var strengthLabel: UILabel! = nil

strengthLabel.text = Navajo.localizedStringForPasswordStrength(Navajo.strengthOfPassword(passwordField.text))

Validating Password

var lengthRule = NJOLengthRule(min: 6, max: 24)
var uppercaseRule = NJORequiredCharacterRule(preset: .LowercaseCharacter)

validator = NJOPasswordValidator(rules: [lengthRule, uppercaseRule])

var failingRules = validator.validatePassword("PASSWORD")

if failingRules == nil {
    NSLog("The password is valid.")
} else {
    var errorMessage = ""

    for var i = 0; i < failingRules!.count; i++ {
        if i > 1 {
            errorMessage += ("\n" + failingRules![i].localizedErrorDescription())
        } else {
            errorMessage += failingRules![i].localizedErrorDescription()
        }
    }

    NSLog("\(errorMessage)")
}

Available Validation Rules

  • 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.

Localization

Keys for the localizable strings Localization Tutorial or check the demo app in the repository.

Password Strength

  • NAVAJO_VERY_WEAK
  • NAVAJO_WEAK
  • NAVAJO_REASONABLE
  • NAVAJO_STRONG
  • NAVAJO_VERY_STRONG

Password Validation

  • 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

TODO

  • Improved documentation
  • Swift Package Manager Support
  • Considering support for Swift throws - catch

Contact

Any feedback and pull requests are welcome :)

Jason Nam
Website
Email

License

Navajo-Swift is available under the MIT license. See the LICENSE file for more info.

About

Password Validator & Strength Evaluator

License:MIT License


Languages

Language:Swift 92.2%Language:Ruby 4.0%Language:Objective-C 3.8%