All Possible Combinations String - Speed [closed]

筅森魡賤 提交于 2019-12-13 09:46:30

问题


I've created a jquery script that generates all possible combinations of a string, where numbers and lower case letters are involved. I only want strings 10 characters long, so I threw in an if statement controlling that.

For those interested, here's the script:

$(document).ready(function(){

    var parts = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'];
    var url = "";

for (var a = 0; a < parts.length; a++) {
    for (var b = 0; b < parts.length; b++) {
        for (var c = 0; c < parts.length; c++) {
            for (var d = c + 1; d < parts.length; d++) {
                for (var e = d + 1; e < parts.length; e++) {
                    for (var f = e + 1; f < parts.length; f++) {
                        for (var g = f + 1; g < parts.length; g++) {
                            for (var h = g + 1; h < parts.length; h++) {
                                for (var i = h + 1; i < parts.length; i++) {
                                    for (var j = i + 1; j < parts.length; j++) {
                                            url = parts[a]+parts[b]+parts[c]+parts[d]+parts[e]+parts[f]+parts[g]+parts[h]+parts[i]+parts[j];
                                            if (url.length === 10) {
                                                $("#URLs").append(url+', ');
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

});

There's a for loop per character spot. If you just want strings 3 char's long, use only 3 nested for's.

It may be inelegant, but it works. My issue is speed/crashes.

Naturally, trying to run this through a browser is a bad idea. What would be a better program/language/set up for speed? I understand that I'm probably bound by my CPU's capabilities no matter what, and that brute forcing is always slow.

If there's a service that does this same thing, I guess that would work too.


回答1:


You're not going to have a place to store the output. You're talking about 3.656e+15 combinations, so your approach of appending that to a DOM element is not going to work.

Even if you used C/C++ or Java, you're still talking about something that's probably infeasible for your storage capacity. And it would take way to long to submit that many requests if you just dynamically submitted them (vs. storing them). I don't think you'll be successful with whatever (shady?) thing you're trying to accomplish here.




回答2:


You're creating 3656158440062976 strings, at 1 million strings per second your script would run for 115 years, at 1 billion strings per second it would be 42 days. And the resulting string (your $('#URLs').append) would be 36 petabytes at 1 byte per character.

I vote not feasible

(Math.pow(36,10)/1000000)/(60*60*24*365)
=115.93602359408219
(Math.pow(36,10)/1000000000)/(60*60*24)
=42.31664861184
(Math.pow(36,10)*10)/(1000*1000*1000*1000*1000)
=36.56158440062976

See wolfram alpha




回答3:


Try reading this, as I believe this is your end goal: http://en.wikipedia.org/wiki/Password_cracking




回答4:


Not to say I'm fully answering your question (there are still performance and memory limitations due to a huge number of permutations), but here's one of the algorithms which crossed my mind: treat the string as a big number in base N, where N is the number of allowed characters.

  1. Start with string aaaaa..aa
  2. If the character at the last position doesn't equal 0 (the last, "biggest" allowed symbol), increment it. Otherwise, set the character to a and increment the previous symbol if it's not equal to 0. Otherwise, ... you get the point.
  3. Repeat step 2 required number of times.

A good thing about this algorithm is that you can wrap it up in a function like getNextString() and call it needed amount of times. Also, there's no recursion, so it should be faster in most languages.




回答5:


What would be a better program/language/set up for speed?

C/C++ would be good language. And use recursion.



来源:https://stackoverflow.com/questions/10130743/all-possible-combinations-string-speed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!