简体   繁体   English

创建一个1337密码检查器(javascript)

[英]Creating a 1337 password checker (javascript)

I'm having an issue with some javascript that I am working on. 我正在处理的一些JavaScript出现问题。 The purpose is to check the input for the list of known bad passwords. 目的是检查输入的已知错误密码列表。 That part was easy. 那部分很简单。 But then it should also be able to check for the "1337" versions of these passwords. 但随后它也应该能够检查这些密码的“ 1337”版本。 Basically it needs to be able to check for numbers in the password and convert that to english, to then run through the known bad passwords function. 基本上,它需要能够检查密码中的数字并将其转换为英语,然后才能通过已知的错误密码功能运行。 I tried to set up a function to do that from Making a leet speak translator but it is only partially working and I am confused as to why, as it is not iterating properly. 我试图从制作一个leet语音翻译器中设置一个功能来做到这一点,但是它仅部分起作用,我对原因感到困惑,因为它没有正确地进行迭代。 Letters that appear more then once only get replaced the first time. 出现次数多于一次的字母仅在第一次被替换。 There is also the issue with l or i being the same number. l或i是相同的数字也存在问题。 I am hoping someone would be able to take a look at it? 我希望有人能够看看它吗?

<script>    
var LettersEnglish = 
    new Array('o', 'i', 'l', 'e', 'a', 's', 't');
var LettersLeet = 
    new Array('0', '1', '1', '3', '4', '5', '7');

function changeLetters() { // change all letters
    var text = document.getElementById("password").value;
    for (var i = 0; i < text.length; i++) {
        text = text.replace(LettersLeet[i], LettersEnglish[i]);
        }englishPasswords(text);
}

function englishPasswords(convertedstring) {
    var myarr = ["password", "letmein", "dragon", "shadow", "joker"];
    var string = (myarr.indexOf(convertedstring) > -1);
    if (string === true){
        document.getElementById("output").innerHTML = convertedstring + " is a bad password";
    }else{
        document.getElementById("output").innerHTML = convertedstring + " is an acceptable password";
    }
}
</script> 

You appear to be iterating over text.length instead of LettersEnglish.length or LettersLeet.length . 您似乎在遍历text.length而不是LettersEnglish.lengthLettersLeet.length This means that if you have a password shorter than the length of these arrays, not all the characters in them will be substituted. 这意味着,如果密码长度短于这些数组的长度,则不会替换其中的所有字符。

@MTCoaster is correct in that you're iterating over the wrong thing. @MTCoaster是正确的,因为您要遍历错误的内容。 You would need something like this instead: 您将需要如下所示:

for (var i = 0; i < LettersLeet.length; i++) {
  text = text.replace(new RegExp(LettersLeet[i], 'g'), LettersEnglish[i]);
}

Note that the replace() call now uses a regular expression as the first argument, and that the regex is created with the "global" flag. 注意, replace()调用现在使用正则表达式作为第一个参数,并且使用“ global”标志创建了正则表达式。 This allows replace() to replace all occurrences of the match. 这允许replace()替换所有匹配项。 The way you call it, with a string as the first argument, only the first occurrence would be replaced. 以字符串作为第一个参数的调用方式,只会替换第一个匹配项。

Regarding the i/l mapping, you're not mapping them both to 1, but rather the other way around, you need 1 to be treated both as i and l when checking for bad passwords. 关于i / l映射,您不是将它们都映射为1,而是反过来,当检查错误密码时,需要将1当作i和l对待。 1 can't possibly map to both at the same time. 1不可能同时映射到两个。 A simplistic approach could be: 一个简单的方法可以是:

var LettersEnglish1 = 
    new Array('o', 'i', 'e', 'a', 's', 't');
var LettersEnglish2 = 
    new Array('o', 'l', 'e', 'a', 's', 't');
var LettersLeet = 
    new Array('0', '1', '3', '4', '5', '7');

and then, inside changeLetters() : 然后,在changeLetters()内部:

var text1 = document.getElementById("password").value,
    text2 = text;
for (var i = 0; i < LettersLeet.length; i++) {
  text1 = text1.replace(new RegExp(LettersLeet[i], 'g'), LettersEnglish1[i]);
  text2 = text2.replace(new RegExp(LettersLeet[i], 'g'), LettersEnglish2[i]);
}
//Now check both text1 and text2 for validity

A complete solution should probably check for all possible permutations of i and l though. 完整的解决方案可能应该检查i和l的所有可能排列。 For example, if the password is 1n1t1a1 , there are 2^4 (16) different replaced passwords to check for. 例如,如果密码为1n1t1a1 ,则需要检查2 ^ 4(16)个不同的替换密码。

Finally, a style advice: 最后,一个样式建议:

var LettersEnglish1 = 
    new Array('o', 'i', 'e', 'a', 's', 't');
var LettersEnglish2 = 
    new Array('o', 'i', 'l', 'e', 'a', 's', 't');
var LettersLeet = 
    new Array('0', '1', '3', '4', '5', '7');

is normally written as 通常写成

var LettersEnglish1 = ['o', 'i', 'e', 'a', 's', 't'],
    LettersEnglish2 = ['o', 'l', 'e', 'a', 's', 't'],
    LettersLeet = ['0', '1', '3', '4', '5', '7'];

Some may prefer multiple var statements like you had it but most use the array literal syntax instead of new Array() . 有些人可能更喜欢像您一样的多个var语句,但是大多数人使用数组文字语法而不是new Array()

Because we had a list of known bad passwords and were later informed there would only be once instance of 1's in the password here is the answer I went with. 因为我们有一个已知的错误密码的列表,后来被告知,密码中只有一个1的实例,这就是我的答案。 For the check of all letters except L, I used @PeterHerdenborg's answer. 为了检查除L以外的所有字母,我使用了@PeterHerdenborg的答案。

var LettersEnglish1 = ['o', 'e', 'a', 's', 't', 'i'],
    LettersLeet = ['0', '3', '4', '5', '7', '1'];

function changeLetters() {
    var text = document.getElementById("password").value;
    for (var i = 0; i < LettersLeet.length; i++) {
        text = text.replace(new RegExp(LettersLeet[i], 'g'), LettersEnglish1[i]);
        //console.log(text);
        englishPasswords(text);
    }
}

function englishPasswords(text) {
    var myarr = ["password", "letmein", "dragon", "shadow", "joker"];
    var string = (myarr.indexOf(text) > -1);
    if (string === true){
        document.getElementById("output").innerHTML = text + " is a bad password";
    }else{
        checkForLs(text);
    }
}

function checkForLs(text){
    text = text.replace('i','l')
    if(text == 'letmein'){
        document.getElementById("output").innerHTML = text + " is a bad password";
    }else{
        document.getElementById("output").innerHTML = text + "thats an acceptable password";
    }
}

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

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