1

在我解决实际问题之前,我想澄清一些事情,因为我知道“使用 javascript 暴力破解”在脱离上下文时听起来是多么荒谬:)。

我正在为我的学士论文工作,最后我的目标是实现一个基于 Javascript 的概念验证哈希破解器。这个想法是这样工作的:用户可以提交一个哈希值以及有关所使用算法的信息。(其他)用户也可以点击网站上的按钮参与破解过程。服务器的任务是接受提交的“订单”并将其拆分为范围,具体取决于可用工人的数量。然后将范围发送给单击所述按钮的客户。

我目前遇到两个大问题,即如何实际实现这个蛮力功能。所以我现在的主要问题是,坦率地说,我还没有真正适应 Javascript。对于初学者,我只会使用硬编码字符集:字母数字、大小写字母,没有特殊字符。问题是老实说,我完全不知道如何实际实现尝试所有字符组合的功能,以及如何编程。我可以想象使用一个包含字符集的普通数组,然后是两个字符串。一个字符串将包含范围,另一个将包含尝试过的组合。所以我不得不以某种方式遍历 charset 数组,而字符串可能使用级联的 for 循环或其他东西,但我真的被“如何”的问题所困扰:)。我不 不希望你们中的任何人真正为我提供这样一个函数的完整源代码(除非你当然想要),但我真的很感激一些关于如何实现这样一个蛮力函数的提示或解释。在这一点上,我也不关心性能或优化编码,而是全面编码,或者你可能想要的任何东西:)

对不起,如果我对我的问题中的一些细节感到模糊。如果是这样,请告诉我,我当然会尝试进一步澄清。

4

1 回答 1

1

字母表上的蛮力风格功能。可能有更简单的方法可以做到这一点。

function brute(alphabet, match, int_start, int_stop){
    var a = alphabet, al = 0,                     // for alphabet
        m = match.toString(), ml = m.length,      // for our compare
        i = int_start || 0, j = int_stop || 0,    // range of numbers to test
        k = 0, l = 0, add = 0, sub = 0, diff = 0, // for building test string
        test = '', found = false;                 // test string and result

    if(i < 0) throw 'int_start must be at least 0';

    if(a.constructor !== undefined){           // We need a string or array as
        if( a.constructor.name !== 'String' && // our alphabet so we check for
            a.constructor.name !== 'Array' )   // correct input and modify if
                a = a.toString();              // necessary, or if we can't, 
    }
    else throw 'Bad alphabet type';            // we throw an error

    al = a.length;    // shorthand length

    add = al;                             // when i=0, we start prefix here
    while(add <= i - sub) sub += add,     // then work out what we have to
                          add = add * al; // prefix our number with

    diff = add - sub; // shorthand to save calculations

    while( i < j ){   // actual brute force loop starts here
        test = '';       // empty any previous string
        k = diff + i;    // convert our number from "x" to "1x"

        while(k > 0){           // build it as a string
            l = k % al;         // get index of digit
            test = a[l] + test; // add digit to string
            k = ( k - l ) / al; // move digits along
        }

        test = test.substring(1); // cut off the initial "1" we added

        if(test.length === ml && test === m){ // compare test to what you want
            found = true;
            break;
        }

        i++;                  // prepare for our next loop
        if(i - sub === add)   // and if we need another digit
            sub += add,       // then recalculate our prefix
            add = add * al,   // and then
            diff = add - sub; // update the shorthand 
    }

    // brute force ended, let's see what we've got

    if(found === false) i = -1; // if not found, return -1 as index

    return [i, test, m]; // index found, string found with, what we were looking for
}

然后通过例如使用

brute('0123abcd', '0c', 0, 20); // [14, "0c", "0c"]
于 2012-09-05T17:57:26.467 回答