https://github.com/jasonnam/navajo-swift
Password Validator & Strength Evaluator
https://github.com/jasonnam/navajo-swift
password-strength password-validation swift
Last synced: 8 days ago
JSON representation
Password Validator & Strength Evaluator
- Host: GitHub
- URL: https://github.com/jasonnam/navajo-swift
- Owner: jasonnam
- License: mit
- Created: 2015-09-08T05:10:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-08-30T01:22:21.000Z (over 3 years ago)
- Last Synced: 2024-10-18T19:29:49.681Z (7 months ago)
- Topics: password-strength, password-validation, swift
- Language: Swift
- Size: 1.58 MB
- Stars: 130
- Watchers: 3
- Forks: 31
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Navajo-Swift
**Password Validator & Strength Evaluator**
> Navajo is named in honor of the famed [code talkers of the Second World War](http://en.wikipedia.org/wiki/Code_talker#Navajo_code_talkers).
## Original Project
[Navajo](https://github.com/mattt/Navajo) by Mattt Thompson
> This project is not compatible with the original project.
## Installation
[](https://apple.com)
[](https://developer.apple.com/swift)
[](https://travis-ci.org/jasonnam/Navajo-Swift)
[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org/pods/Navajo-Swift)### Carthage
```ogdl
github "jasonnam/Navajo-Swift"
```### CocoaPods
```ruby
use_frameworks!
pod 'Navajo-Swift'
``````swift
import Navajo_Swift
```### Swift Package Manager:
Add the following line to your `Package.swift`:
```swift
dependencies: [
// ...
.package(url: "https://github.com/Wistas23/Navajo-Swift.git"),
],
```### Manual
Just copy the files in Source folder into your project.
## Usage
### Evaluating Password Strength
> Password strength is evaluated in terms of [information entropy](http://en.wikipedia.org/wiki/Entropy_%28information_theory%29).
```swift
let password = passwordField.text ?? ""
let strength = Navajo.strength(ofPassword: password)strengthLabel.text = Navajo.localizedString(forStrength: strength)
```### Validating Password
```swift
let lengthRule = LengthRule(min: 6, max: 24)
let uppercaseRule = RequiredCharacterRule(preset: .LowercaseCharacter)validator = PasswordValidator(rules: [lengthRule, uppercaseRule])
if let failingRules = validator.validate(password) {
validationLabel.textColor = .red
validationLabel.text = failingRules.map({ return $0.localizedErrorDescription }).joined(separator: "\n")
} else {
validationLabel.textColor = .green
validationLabel.text = "Valid"
}
```#### Available Validation Rules
- Allowed Characters
- Required Characters (custom, lowercase, uppercase, decimal, symbol)
- Non-Dictionary Word
- Minimum / Maximum Length
- Predicate Match
- Regular Expression Match
- Block EvaluationIf 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.
```swift
let rule = PredicateRule(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](http://rshankar.com/internationalization-and-localization-of-apps-in-xcode-6-and-swift/) 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## Contact
Any feedback and pull requests are welcome :)
Jason Nam
[Website](http://jasonnam.com)
[Email](mailto:[email protected])## License
Navajo-Swift is available under the MIT license. See the LICENSE file for more info.