From 83a62b1b5cdd01077c701498e8f6a9f567af56da Mon Sep 17 00:00:00 2001 From: aixiao Date: Wed, 1 Jun 2022 13:37:23 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E5=AF=86=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- random_password.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 random_password.c 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; +} +