regex 如何验证密码中是否包含名称

3wabscal  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(120)

我有一个问题,我需要防止用户提供的密码包含他的名字或部分名字。规则如下:
1.密码中不能包含任何用户的名字或姓氏
1.密码中的用户名或姓不能有大于或等于3个字符的部分。
到目前为止,我的代码只能验证规则1。

fun containsUsername(data: PasswordData): Boolean {
    val partsUsername = data.username.split(" ").joinToString("|")
    val namePattern = Regex("""(?i).*($partsUsername).*""")

    return namePattern.containsMatchIn(data.password)
}

举例说明以便于理解:

fun main() {
     val username = "Maria Antonia dos Santos"
     val password1 = "AbcMariaXyz" // Invalid password due to "Maria"
     val password2 = "AntoniaMaria" // Invalid password due to "Maria"
     val password3 = "Maria123" // Invalid password due to "Maria"
     val password4 = "AntoniaSenha" // Invalid password due to "Antonia"
     val password5 = "Dos_123" // Invalid password due to "Dos"
     val password6 = "SantosAbc" // Invalid password due to "Santos"
     val password7 = "OniSenha" // Invalid password due to "Oni"
     val password8 = "MarSenha" // Invalid password due to "Mar"
    
     println(containsUsername(password1, username)) // true
     println(containsUsername(password2, username)) // true
     println(containsUsername(password3, username)) // true
     println(containsUsername(password4, username)) // true
     println(containsUsername(password5, username)) // true
     println(containsUsername(password6, username)) // true
     println(containsUsername(password7, username)) // true
     println(containsUsername(password8, username)) // true
}
xnifntxz

xnifntxz1#

如果找不到合适的regex解决方案,您也许可以搜索密码字符串。这是在javascript(问题 * 被 * 标记),因为我知道kotlin甚至比我知道JavaScript更糟糕,但如果这个解决方案对你有用的话,希望转换它并不难。

function containsUsername(name, password) {
  // convert the strings to lowercase for easier searching
  // spaces should apparently not count, so split the name into parts
  nameparts = name.toLowerCase().split(" ");
  lpwd = password.toLowerCase();
  
  for(part of nameparts) {
    // check at most 3 characters at a time from each part
    end = part.length > 3 ? part.length - 3 : 0;
    for(i = 0; i <= end; ++i) {
      if(lpwd.indexOf(part.substr(i, 3)) != -1) return true;
    }
  }
  return false; // no match found
}

username = "Maria Antonia dos Santos";
  
const passwords = [
  "AbcMariaXyz", // Invalid password due to "Maria"
  "AntoniaMaria", // Invalid password due to "Maria"
  "Maria123", // Invalid password due to "Maria"
  "AntoniaSenha", // Invalid password due to "Antonia"
  "Dos_123", // Invalid password due to "Dos"
  "SantosAbc", // Invalid password due to "Santos"
  "OniSenha", // Invalid password due to "Oni"
  "MarSenha", // Invalid password due to "Mar"
];
     
for (pwd of passwords) {
  console.log("testing " + pwd);
  res = containsUsername(username, pwd);
  console.log(res);
}

这里有一个更详细的版本,它返回匹配的字符串部分,或者如果密码没有命中,则返回空字符串。我添加了一些调试原则,以便更容易看到它在做什么:

function containsUsername(name, password) {
  // convert the strings to lowercase for easier searching
  // spaces should apparently not count, so split the name into parts
  nameparts = name.toLowerCase().split(" ");
  lpwd = password.toLowerCase();
  
  for(part of nameparts) {
    // check at most 3 characters at a time from each part
    console.log(" Trying substrings of \"" + part + "\"");
    end = part.length > 3 ? part.length - 3 : 0;
    for(i = 0; i <= end; ++i) {
      subpart = part.substr(i, 3);
      console.log("  checking \"" + subpart + "\"");
      if(lpwd.indexOf(subpart) != -1) return subpart;
    }
  }
  return ""; // no match found
}

username = "Maria Antonia dos Santos";
  
const passwords = [
  "AbcMariaXyz", // Invalid password due to "Maria"
  "AntoniaMaria", // Invalid password due to "Maria"
  "Maria123", // Invalid password due to "Maria"
  "AntoniaSenha", // Invalid password due to "Antonia"
  "Dos_123", // Invalid password due to "Dos"
  "SantosAbc", // Invalid password due to "Santos"
  "OniSenha", // Invalid password due to "Oni"
  "MarSenha", // Invalid password due to "Mar"
  "A An", // Valid
];
     
for (pwd of passwords) {
  console.log("Testing password \"" + pwd + "\"");
  res = containsUsername(username, pwd);
  console.log(" Verdict: " + (res.length ? "NOK, \"" + res + "\" found in \"" + pwd + "\"" : "OK"));
}

相关问题