Polish codes related to password generation

1. Polish codes in PasswordGeneratorFlavour
2. Polish related codes in view controllers
This commit is contained in:
Yishi Lin 2020-02-22 23:23:02 +08:00
parent ae94388ba4
commit 71c793029a
10 changed files with 108 additions and 127 deletions

View file

@ -19,6 +19,8 @@ public enum GitAuthenticationMethod: String, DefaultsSerializable {
case password, key
}
extension PasswordGeneratorFlavor: DefaultsSerializable {}
public extension DefaultsKeys {
var pgpKeySource: DefaultsKey<KeySource?> { .init("pgpKeySource") }
var pgpPublicKeyURL: DefaultsKey<URL?> { .init("pgpPublicKeyURL") }
@ -50,7 +52,7 @@ public extension DefaultsKeys {
var isShowFolderOn: DefaultsKey<Bool> { .init("isShowFolderOn", defaultValue: true) }
var isHidePasswordImagesOn: DefaultsKey<Bool> { .init("isHidePasswordImagesOn", defaultValue: false) }
var searchDefault: DefaultsKey<SearchBarScope?> { .init("searchDefault", defaultValue: .all) }
var passwordGeneratorFlavor: DefaultsKey<String> { .init("passwordGeneratorFlavor", defaultValue: "Apple") }
var passwordGeneratorFlavor: DefaultsKey<PasswordGeneratorFlavor> { .init("passwordGeneratorFlavor", defaultValue: .apple) }
var encryptInArmored: DefaultsKey<Bool> { .init("encryptInArmored", defaultValue: false) }
}

View file

@ -0,0 +1,52 @@
//
// PasswordGeneratorFlavour.swift
// passKit
//
// Created by Danny Moesch on 28.11.18.
// Copyright © 2018 Bob Sun. All rights reserved.
//
import KeychainAccess
public enum PasswordGeneratorFlavor: String {
case apple = "Apple"
case random = "Random"
public var localized: String {
return rawValue.localize()
}
public var longNameLocalized: String {
switch self {
case .apple:
return "ApplesKeychainStyle".localize()
case .random:
return "RandomString".localize()
}
}
public var defaultLength: (min: Int, max: Int, def: Int) {
switch self {
case .apple:
return (15, 15, 15)
case .random:
return (4, 64, 16)
}
}
public func generate(length: Int) -> String {
switch self {
case .apple:
return Keychain.generatePassword()
case .random:
return PasswordGeneratorFlavor.generateRandom(length: length)
}
}
private static func generateRandom(length: Int) -> String {
let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
return String((0..<length).map { _ in chars.randomElement()! })
}
}
extension PasswordGeneratorFlavor: CaseIterable {}

View file

@ -1,48 +0,0 @@
//
// 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 name: String {
return rawValue.localize()
}
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 {}