随机密码生成

This commit is contained in:
2022-06-01 13:37:23 +08:00
commit 83a62b1b5c

55
random_password.c Normal file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#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;
}