'Password Generator Using Multiple Strings in Swift
I'm new to coding and I'm trying to create a password generator. I've created one using a single array of strings (all lowercase or all uppercase). But I want to create using multiple arrays. I'm using Swift 5.3 (Xcode 13.2.1)
struct ContainCharacterSelection {
var containNumbers: Bool = true
var containLowerCharacters: Bool = true
var containUpperCharacters: Bool = true
var containSpecialCharacters: Bool = true
var containComplicatedCharacters: Bool = false
}
class PasswordGenerator {
let numbers = ["0","1","2","3","4","5","6","7","8","9"]
let lowerCharacters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let upperCharacters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
let specialCharacters = ["!","\"","§","$","%","&","/","(",")","=","?","+","*","#",",",";",".",":","-","_","@","<",">"]
let complicatedCharacters = ["^","[","]","{","}","\\","'","`","´"]
var password = ""
var passwordLenght = sliderValue
}
Thanks in advance.
Solution 1:[1]
Join an array of strings into a single string.
let numbers = ["0","1","2","3","4","5","6","7","8","9"] let joinedNumbers = array.joined() //joinedNumbers equals to 0123456789Join other arrays via the same way:
joinedLowerCharacters,joinedUpperCharacters.....Then we concatenate these strings as a single string.
finalString = joinedNumbers+joinedLowerCharacters+joinedUpperCharacters+...
Now we could generate password using the finalString.
Solution 2:[2]
Something like this would work. This is relatively simple and does not actually check the existence of certain type of character. If you need to absolutely verify the existence of certain character type, you might have to do some weird recursion logic.
However, this should work in most of the simpler cases.
struct ContainCharacterSelection {
var containNumbers: Bool = true
var containLowerCharacters: Bool = true
var containUpperCharacters: Bool = true
var containSpecialCharacters: Bool = true
var containComplicatedCharacters: Bool = false
}
class PasswordGenerator {
let numbers = ["0","1","2","3","4","5","6","7","8","9"]
let lowerCharacters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
let upperCharacters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
let specialCharacters = ["!","\"","§","$","%","&","/","(",")","=","?","+","*","#",",",";",".",":","-","_","@","<",">"]
let complicatedCharacters = ["^","[","]","{","}","\\","'","`","´"]
var password = ""
var passwordLength = 32
func generate(selections: ContainCharacterSelection) {
var includedChars: [String] = [];
if selections.containLowerCharacters {
includedChars += lowerCharacters;
}
if selections.containUpperCharacters {
includedChars += upperCharacters;
}
if selections.containNumbers {
includedChars += numbers;
}
if selections.containSpecialCharacters {
includedChars += specialCharacters
}
if selections.containComplicatedCharacters {
includedChars += complicatedCharacters
}
includedChars.shuffle()
// mind it that this could crash if your array is smaller than the passwordLength, in that case use alternative solution commented below
self.password = includedChars[..< passwordLength].joined(separator: "")
// let randoms = (0 ..< passwordLength)
// .map { _ in includedChars.randomElement() }
// .compactMap { $0 }
// self.password = randoms.joined(separator: "")
}
}
var selections = ContainCharacterSelection()
selections.containUpperCharacters = true
selections.containLowerCharacters = true
selections.containSpecialCharacters = true
let passwordGenerator = PasswordGenerator()
passwordGenerator.generate(selections: selections)
print("Password: ", passwordGenerator.password)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | navylover |
| Solution 2 |
