I am making a Swift program to stress test certain pieces of computer hardware for a research project I am working on. One of the programs I am making is guessing a password. I currently have an implementation that works by doing it recursively.
func passwordCracker(_ password: String, _ guess: String) {
for letter in letters {
let newGuess = guess + letter
tryCount = tryCount + 1
if (password == newGuess) {
let stop = DispatchTime.now().rawValue
print("the password is \(newGuess) and it took \((stop - start)/1_000_000_000) seconds and \(tryCount) guesses.")
//return
//This causes the program to "crash" ending all the other recursive calls.
//We use this instead of return as return will still continue to do other recursive calls after the fact
exit(0)
}
}
if(guess.count + 1 >= depth) { return }
for letter in letters {
passwordCracker(password, guess + letter)
}
}
I want to create a way of doing it non-recursively but have been unable to do so. My current implementation works only when the password to be guessed is in alphabetical order (abc, abcd, etc.). Would welcome any suggestions on how to fix it.
func iterative(_ pwd: String) {
var guess = ""
for _ in 0..<depth {
for letter in letters {
guess = guess + letter
for l in letters {
if guess + l == pwd {
print("Password found. It is: \(pwd)")
return
}
}
}
}
print("Password NOT found")
}