commit 83a62b1b5cdd01077c701498e8f6a9f567af56da Author: aixiao Date: Wed Jun 1 13:37:23 2022 +0800 随机密码生成 diff --git a/random_password.c b/random_password.c new file mode 100644 index 0000000..7ecbd4d --- /dev/null +++ b/random_password.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZ 270 + + +char pool[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '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', '!', '@', '#', '$', '%', '^', '&', '*', + '[', ']', '\\', '|', ';', '\'', ',', '.', '/', '{', '}', ':', '"', '<', '>', '?' + + +}; //随机池 + +int main(int argc, char *argv[]) +{ + // srand(time(0)); + /* + 使用 time(0) 做种子,在一秒之内种子是不会变的 + 通过使用微妙,来增加随机数的随机性 + */ + + int PASSWD_LEN = 16; + struct timeval tpstart; + char password[BUFFER_SIZ]; + int i = 0; + + + if (argv[1] != NULL) + { + PASSWD_LEN = atoi(argv[1]); + } + memset(password, 0, BUFFER_SIZ); + gettimeofday(&tpstart, NULL); + srand(tpstart.tv_usec); + + + while (i != PASSWD_LEN) { + password[i++] = pool[rand() % sizeof(pool)]; + } + + printf("%s\n", password); + + return 0; +} +