因帮朋友找回密码,需要生成一个密码字典,然后就写了一个生成函数。找回过程中,感叹密码如果太短,真的是一点安全都没有。下面请看测试用例。
生成函数
words参数:二维数组,长度不限。
function generatePasswordDictionary(words) {
const dictionary = [];
(function generate(index, parentWord) {
if (index >= words.length) return dictionary.push(parentWord);
words[index].forEach((currentWord) => {
generate(index + 1, parentWord + currentWord);
});
})(0, "");
return dictionary;
}
使用方式

生成结果
const fs = require("fs");
// 根据情况传入参数即可。
const dictionary = generatePasswordDictionary([
["i", "I","love", "Love", "LOVE","My", "MY", "my", "mY","country", "counTry", "countrY"],
["i", "I","love", "Love", "LOVE","My", "MY", "my", "mY","country", "counTry", "countrY"],
["i", "I","love", "Love", "LOVE","My", "MY", "my", "mY","country", "counTry", "countrY"],
["i", "I","love", "Love", "LOVE","My", "MY", "my", "mY","country", "counTry", "countrY"],
]);
console.log(dictionary);
//输出到文件
fs.writeFile("./password.txt", dictionary.join("\n"), () => {});
人人为我,我为人人,谢谢您的浏览,我们一起加油吧。