HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Finding a password with brute force

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
forcewithpasswordfindingbrute

Problem

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 answer = 0;
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 -1 ) {
startingChar = i;
//alert("starting:"+startingChar);
}
if(currentCharCode.search(endingChar) > -1) {
endingChar = i;
//alert("Ending:"+endingChar);
}

}
//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"+tempStore.replace(0,"")+"";
if(tempStore.replace(0,"") == ans) {
window.clearInterval(runBrute);
document.getElementById('outputDisplay').innerHT

Solution

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.

Code Snippets

for(var i = 33; i<125;i++) {
for(var i = 33, x = 0; i < 125; i++, x++) {
return window.setInterval(bruteHackSolve,0025);
for(i=0;i<maxChar;i++) {
for(var i = 0; i < maxChar; i++) {

Context

StackExchange Code Review Q#75446, answer score: 6

Revisions (0)

No revisions yet.