From ad2d69edca473eadcff0078673fdef1626b2c8eb Mon Sep 17 00:00:00 2001 From: aixiao Date: Thu, 16 Jun 2022 11:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- random_password.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/random_password.c b/random_password.c index 7ecbd4d..fbe9d9a 100644 --- a/random_password.c +++ b/random_password.c @@ -5,7 +5,7 @@ #include #include -#define BUFFER_SIZ 270 +#define BUFFER_SIZ 8192 char pool[] = { @@ -17,18 +17,11 @@ char pool[] = { '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]; @@ -39,11 +32,17 @@ int main(int argc, char *argv[]) { PASSWD_LEN = atoi(argv[1]); } + + if (PASSWD_LEN >= BUFFER_SIZ) + { + printf("Password too long!\n"); + exit(1); + } + memset(password, 0, BUFFER_SIZ); gettimeofday(&tpstart, NULL); srand(tpstart.tv_usec); - while (i != PASSWD_LEN) { password[i++] = pool[rand() % sizeof(pool)]; }