作为对强制密码的关于信息安全的一个问题的回应,我编写了帮助解决这个问题的代码:
生成一个包含10个字符的密码列表,其中只包含3-6个数字和3-6大写字母的组合。
我希望对我编写的代码片段进行代码评审。我对优化软件一点也不了解。我可以写它(自学),但我没有深入的洞察力来改进已经在工作的软件,所以我已经开始在这里张贴代码片段,让你们给我提供洞察力。我真的认为这个频道很棒,不用再费吹灰之力了,我会找到它的。
#include <iostream>
#include <vector>
#include <random>
#include <string>
const char charset[] = {'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','0','1','2','3','4','5','6','7','8','8','9'};
int main()
{
std::cout << "Please enter the number of passwords to generate here: ";
int num_pass;
std::cin >> num_pass;
std::random_device dev;
std::mt19937_64 rng(dev());
std::vector<std::string> passwds;
std::uniform_int_distribution<std::mt19937_64::result_type> dist(0, sizeof(charset) - 1);
for (int i = 0; i < num_pass; ++i) {
std::string pass = "";
int num_nums = 0, num_chars = 0;
while (pass.length() < 10) {
char c = charset[dist(rng)];
if (isdigit(c) && num_nums < 6) {
pass += c;
num_nums++;
}
else if (isalpha(c) && num_chars < 6) {
pass += c;
num_chars++;
}
}
passwds.push_back(pass);
std::cout << pass << std::endl;
}
std::cin.get();
}
```
发布于 2019-05-09 01:07:48
您的密码会有偏见,更多的字母出现在密码的前面,更多的数字出现在后面。
更好的方法是确定密码中有多少位数字。然后,对于每个字符,通过检查if (随机(Characters_left)< digits_left),根据您希望拥有的数字数和要填充的字符数来确定它是否应该是一个数字。然后为该位置选择随机数字或字母。
声明pass
时,不需要传递空字符串。它是默认构造为空的。
std::string pass;
https://codereview.stackexchange.com/questions/219954
复制相似问题