Introduce enum for password generator flavour

This commit is contained in:
Danny Moesch 2018-11-28 22:58:24 +01:00 committed by Bob Sun
parent 1f57305203
commit 3cd8df310c
10 changed files with 102 additions and 47 deletions

View file

@ -0,0 +1,44 @@
//
// PasswordGeneratorFlavour.swift
// passKit
//
// Created by Danny Moesch on 28.11.18.
// Copyright © 2018 Bob Sun. All rights reserved.
//
import KeychainAccess
public enum PasswordGeneratorFlavour: String {
case APPLE = "Apple"
case RANDOM = "Random"
private static let ALLOWED_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
public static func from(_ option: String) -> PasswordGeneratorFlavour {
return PasswordGeneratorFlavour(rawValue: option) ?? PasswordGeneratorFlavour.RANDOM
}
public var defaultLength: (min: Int, max: Int, def: Int) {
switch self {
case .APPLE:
return (15, 15, 15)
default:
return (4, 64, 16)
}
}
public func generatePassword(length: Int) -> String {
switch self {
case .APPLE:
return Keychain.generatePassword()
default:
return PasswordGeneratorFlavour.randomString(length: length)
}
}
private static func randomString(length: Int) -> String {
return String((0..<length).map { _ in ALLOWED_CHARACTERS.randomElement()! })
}
}
extension PasswordGeneratorFlavour: CaseIterable {}