# Simple bruteforce algorithm in Swift

So, I am learning Swift and as a little exercise I tried to implement a BruteForce algorithm. That algorithm is pretty simple however it is a bit slow too… Well, I am new to Swift, so maybe some optimisations are possible ?

``````extension String {
var digits:      String { return "0123456789" }
var lowercase:   String { return "abcdefghijklmnopqrstuvwxyz" }
var uppercase:   String { return "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }
var punctuation: String { return "!"#\$%&'()*+,-./:;<=>?@(\)^_`{|}~" }
var letters:     String { return lowercase + uppercase }
var printable:   String { return digits + letters + punctuation }

mutating func replace(at index: Int, with character: Character) {
var stringArray = Array(self)
stringArray(index) = character
self = String(stringArray)
}
}
``````

Let’s now implement the BruteForce algorithm

``````let ALLOWED_CHARACTERS:   (String) = String().printable.map { String(\$0) }
let MAXIMUM_PASSWORD_SIZE:   Int   = 3

func indexOfCharacter(_ character: Character, inArray array: (String)) -> Int {
return array.firstIndex(of: String(character))!
}

func characterAtIndex(_ index: Int, inArray array: (String)) -> Character {
return index < array.count ? Character(array(index))
: Character("")
}

func generateBruteForce(_ string: String, ofArray array: (String)) -> String {
var str: String = string

if str.count <= 0 {
str.append(characterAtIndex(0, inArray: array))
}
else {
str.replace(at: str.count - 1,
with: characterAtIndex((indexOfCharacter(str.last!, inArray: array) + 1) % array.count, inArray: array))

if indexOfCharacter(str.last!, inArray: array) == 0 {
str = String(generateBruteForce(String(str.dropLast()), ofArray: array)) + String(str.last!)
}
}

return str
}
``````

Now we can use it that way

``````import Foundation

var password: String = ""

// Will strangely ends at 0000 instead of ~~~
while password.count <= MAXIMUM_PASSWORD_SIZE { // Increase MAXIMUM_PASSWORD_SIZE value for more
// Your stuff here
// Your stuff here
}
``````

It will print

``````0
1
2
(...)
{
|
}
~
00
01
and so on...
``````