简体   繁体   English

查找JavaScript字符串的所有组合

[英]Finding All Combinations for a JavaScript String

I just read an interesting article Bruce Schneier - Choosing Secure Passwords and came up with the idea of finding out how long it would take JavaScript to "crack" my own password (a string/ the function parameter). 我刚刚读了一篇有趣的文章Bruce Schneier-选择安全密码,然后想到了找出JavaScript“破解”我自己的密码(字符串/函数参数)需要多长时间。

How can I write a function, that simply compares all possible combinations of letters (and later numbers, maybe special characters etc.) with a certain input parameter (type is string) of that function. 我如何编写一个函数,该函数将字母(以及后面的数字,也许是特殊字符等)的所有可能组合与该函数的某个输入参数(类型为字符串)进行比较。 The possible characters will be listed in an array. 可能的字符将在数组中列出。

eg: 例如:

var letterDB = ["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", "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"];
// actually I would like to use a Regular Expression instead of my own letterDB, but for the beginning it is fine.

I could find a solution for input parameters of only 1 letter (but I am still missing to pass longer strings into that function). 我可以找到仅1个字母的输入参数的解决方案(但是仍然缺少将更长的字符串传递给该函数的方法)。 It is very confusing how to handle the problem of strings like "cloud", "play", etc. I am thinking about solving this issue with a for-loop but I don't really know where to start with the loop and how I programm loops for inputs that I dont' know the length yet. 如何处理诸如“ cloud”,“ play”等字符串的问题非常令人困惑。我正在考虑通过for循环解决此问题,但我真的不知道从哪里开始以及如何循环我不知道长度的程序循环输入。

My function: 我的功能:

function crack(x) {
  var timerStart = Date.now();  //for curiosity I want to log the time of the entire calculation
  var s = String(x);            //make sure the input is of type string (gets interesting when numbers are used in the string)

  for(i=0; i < letterDB.length; i++) {
      if (s == letterDB[i]) {
          console.log("found same combination as your input");
          console.log("letter was: " + letterDB[i]);
          console.log("interval: " + i);
          console.log("calculation time:", Date.now() - timerStart);  //interesting to see the time of the calculation when my input parameters will get longer and harder to crack because of special characters.
      }
  }
}

You can simply loop through the string and iterate over all the letters: 您可以简单地遍历字符串并遍历所有字母:

function crack(x) {
    var timerStart = Date.now();
    x = x.toString();
    var charsArr = s.split('');
    var result = charsArr.every(function(char) {
        return letterDB.includes(char);
    });
    if (result) {
        console.log("Success! calculation time:", Date.now() - timerStart);
    }
    console.log("fail");
    return result;
}

If result is true, then it means that you can build the string from the letterDB array. 如果result为true,则意味着您可以从letterDB数组构建字符串。

As said in the comments, this will take forever. 正如评论中所说,这将是永远的。 However, it's relatively simple to implement a permutation-based cracker with recursion. 但是,使用递归实现基于置换的破解器相对简单。

var input = "ad";
var done = false;
var maxLength = 2;
var letterDB = ["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", "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"];

function recursion (str) {
   for (var i=0; i < letterDB.length; i++) {
      var test = str+letterDB[i];
      if (test !== input && !done && str.length < maxLength) {
         recursion(test);
      } else if (test === input) {
         done = true;
         alert("your pass is "+test);
      }
   }
}
recursion("");

To gauge how slow this is, the password cloud took me 33 seconds to find (with maxlength correctly set). 为了衡量这有多慢,密码cloud花了我33秒的时间找到(正确设置了maxlength)。 The time would escalate extremely quickly for anything with a larger length. 对于任何长度较大的内容,时间将极其迅速地增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM