# Finding a password with brute force

The purpose of this code is to find a password with brute force cracking where as it will try all the possible combinations until it finds the correct password. For example, it will start like a then b then c . . . ab . . . cz . . . dd and so on until it finds the correct password. All of it works, but the performance is rather slow after it runs for like 20 seconds; it starts to become choppy. I am looking for how to improve the performance of this so it will remain running constantly at 40 fps+.

I feel like when I run this code it must create memory leaks and lag. I'm just goofing off to learn a little and use my free time, but I would prefer if this ran more smooth. I am not sure how to detect memory leaks well, but if someone can point mine out I should be able to understand it better.

This isn't anything to be used for illegal use, it is just for pure fun.

//Brute Hack js
var wholeList = [];
var runBrute = null;
var charList = null;
var minChar = null;
var maxChar = null;
var a=0;
var b=0;
var c=0;
var d=0;
var e=0;
var f=-1;
var possibilities = 0;
var possAns = [];
var ans = null;

function bruteHack(startingChar,minCha,maxCha, endingChar) {
minChar = minCha;
maxChar = maxCha;
x = 0;
for(var i = 33; i<125;i++) {
currentCharCode = String.fromCharCode (i);
//Compiles wholeList
wholeList[x] = currentCharCode;
x++;
//Find num val of starting and end char
if(currentCharCode.search(startingChar) > -1 ) {
startingChar = i;
}
if(currentCharCode.search(endingChar) > -1) {
endingChar = i;
}

}
//33 to 125 charCode
if(startingChar != null && endingChar != null) {
charToSearchFor = (endingChar - startingChar); // How many chars to find between the two points
charList = []; //Storing my list
//Compile my array of vars
x = startingChar;
for(var i = 0;i<=charToSearchFor;i++) {

currentCharCode = String.fromCharCode (x);
charList[i] = currentCharCode;
x++;
}
runBrute = intervalTrigger(); //Invokes our solver interval
}
return wholeList;

}
function intervalTrigger() {
return window.setInterval(bruteHackSolve,0025);
}

function bruteHackSolve() {
if( answer != 1 ) {
//Init vars
for(i=0;i<maxChar;i++) {
possAns[i] = 0;
}
for(i=0;i<minChar;i++) {
possAns[maxChar-i] = charList[0];
}
for(i=minChar;i<=maxChar;i++) {
possibilities+= Math.pow(charList.length,i); //Amount of possible entry codes
}
//End of init vars
for (i=0; i< maxChar;i++) {
if(i == 0) {
ans = wholeList[Math.floor(Math.random() * wholeList.length)];
}
else {
ans = ans.concat(wholeList[Math.floor(Math.random() * wholeList.length)]);
}
}
}
if (ans != null) {
//Start the process to find the passcode

f++;
possAns[maxChar] = charList[f];
if(f==charList.length) {
f=0;
e++;
possAns[maxChar-1] = charList[e];
}
else if(e==charList.length) {
e=0;
d++;
possAns[maxChar-2] = charList[d];
}
else if(d==charList.length) {
d=0;
c++;
possAns[maxChar-3] = charList[c];
}
else if(c==charList.length) {
c=0;
b++;
possAns[maxChar-4] = charList[b];
}
else if(b==charList.length) {
b=0;
a++;
possAns[maxChar-5] = charList[a];
}
tempStore = possAns.join("");
document.getElementById('outputDisplay').innerHTML = "<p>"+tempStore.replace(0,"")+"</p>";
if(tempStore.replace(0,"") == ans) {
window.clearInterval(runBrute);
document.getElementById('outputDisplay').innerHTML = "<p>"+ans+"</p>";
}
}
}


# Variable names

@dec already touched on this a little bit. Variable names are meant to describe what that variable is to be used for. Letter variables(such as a, b, and c) don't really describe anything. Also, (dec mentioned this too) don't use take a variable name, drop a letter, and create another variable with that new name - it makes your code confusing as the purpose of the variable is un-clear.

# For loop

I notice how, in your bruteHack's for loop, you kind of split it up. You had the variable i being declared, defined, conditionally checked, and incremented all in the signature of your for loop. But then you declared a variable x outside of the loop, and incremented it in the body of the loop. Why don't you just put x in the signature along with i?

This:

for(var i = 33; i<125;i++) {


Becomes:

for(var i = 33, x = 0; i < 125; i++, x++) {


# Octal and decimal

This might be intended, but I just wanted to point it out. In your function intervalTrigger, you have the line:

return window.setInterval(bruteHackSolve,0025);


I don't know why you have the two trailing 0's on the interval - JavaScript reads a trailing 0 as an octal number. 0025 in octal is equal to 21 decimal.

# Global variable

In your function bruteHackSolve, in the first for loop, you declare the variable i, but you make it global - as in, you didn't append var to the beginning. This is not good practice, and can cause errors if other functions or scripts on a page are using the same variable name.

What you wrote:

for(i=0;i<maxChar;i++) {


What it should be:

for(var i = 0; i < maxChar; i++) {


However, for the for loops after that, you don't need to type var i again, as it was already declared locally.

# Switches over if/else's

Also in your bruteHackSolve function, you have a lot of if's, else's, and else if's right after each other that all compare to the same thing: charList.length. It would be a lot easier (and I'm assuming, a lot more efficient) if you created a switch statement:

switch(charList.length) {
case f: // if f == charList.length
/* code */
break;
case e:
/* code */
break;
case d:
/* code */
break;
case c:
/* code */
break;
case b:
/* code */
break;
}


# Repetitive-ness

In your bruteHack function, towards the bottom, you wrote: x = startingChar. Looking at the rest of the function after that line, you don't use startingChar at all, so why set x to it? You could just use startingChar itself.

# Final notes

If I find any more things to point out, I'll update my post.

• In the nit-picking section: Never, ever do that. Horrible bugs may occur. Commented Jan 25, 2015 at 22:43
• Thank you, @MannyMeng. I will leave that up there and mention your comment as a reference for readers. Commented Jan 25, 2015 at 22:52
• What you propose about Variable declarations doesn't look simpler at all. It's less clear, longer, and you have to type each variable name twice, which can be error prone too, the same way that any kind of code duplication is error prone. And your nit-pick about not-using braces for single-statement for loops goes against the general recommendation to always use braces, followed by most reviewers around here. This may be a matter of taste though. But one thing is sure: always using braces is safer. I suggest to drop these two points, and I'll be happy to upvote. Commented Jan 26, 2015 at 6:27
• @janos I removed the two sections Commented Jan 26, 2015 at 20:08
• @SirPython I'm glad you reconsidered! And, +1 ;-) Commented Jan 26, 2015 at 20:13

Try to name variables so that they express what they mean: answer -> createRandomAnswer.

Don't use name that are almost equal to other names: minChar = minChar.