Introduce enum for password generator flavour
This commit is contained in:
parent
1f57305203
commit
3cd8df310c
10 changed files with 102 additions and 47 deletions
|
|
@ -39,9 +39,6 @@ public class Globals {
|
|||
public static let iTunesFileSharingPGPPrivate = iTunesFileSharingPath + "/gpg_key"
|
||||
public static let iTunesFileSharingSSHPrivate = iTunesFileSharingPath + "/ssh_key"
|
||||
|
||||
public static let passwordDefaultLength = ["Random": (min: 4, max: 64, def: 16),
|
||||
"Apple": (min: 15, max: 15, def: 15)]
|
||||
|
||||
public static let gitSignatureDefaultName = "Pass for iOS"
|
||||
public static let gitSignatureDefaultEmail = "user@passforios"
|
||||
|
||||
|
|
|
|||
44
passKit/Helpers/PasswordGeneratorFlavour.swift
Normal file
44
passKit/Helpers/PasswordGeneratorFlavour.swift
Normal 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 {}
|
||||
|
|
@ -53,11 +53,6 @@ public class Utils {
|
|||
}
|
||||
return attributedPassword
|
||||
}
|
||||
public static func initDefaultKeys() {
|
||||
if SharedDefaults[.passwordGeneratorFlavor] == "" {
|
||||
SharedDefaults[.passwordGeneratorFlavor] = "Random"
|
||||
}
|
||||
}
|
||||
|
||||
public static func alert(title: String, message: String, controller: UIViewController, handler: ((UIAlertAction) -> Void)? = nil, completion: (() -> Void)? = nil) {
|
||||
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@
|
|||
import SwiftyUserDefaults
|
||||
import OneTimePassword
|
||||
import Base32
|
||||
import KeychainAccess
|
||||
|
||||
public class Password {
|
||||
|
||||
|
|
@ -276,31 +275,4 @@ public class Password {
|
|||
// get and return the password
|
||||
return self.otpToken?.currentPassword
|
||||
}
|
||||
|
||||
public static func generatePassword(length: Int) -> String{
|
||||
switch SharedDefaults[.passwordGeneratorFlavor] {
|
||||
case "Random":
|
||||
return randomString(length: length)
|
||||
case "Apple":
|
||||
return Keychain.generatePassword()
|
||||
default:
|
||||
return randomString(length: length)
|
||||
}
|
||||
}
|
||||
|
||||
private static func randomString(length: Int) -> String {
|
||||
|
||||
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
|
||||
let len = UInt32(letters.length)
|
||||
|
||||
var randomString = ""
|
||||
|
||||
for _ in 0 ..< length {
|
||||
let rand = arc4random_uniform(len)
|
||||
var nextChar = letters.character(at: Int(rand))
|
||||
randomString += NSString(characters: &nextChar, length: 1) as String
|
||||
}
|
||||
|
||||
return randomString
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue