}
+static int generate_random_num(unsigned short* buf, int len) {
+ int fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ if (read(fd, buf, len) < 0)
+ return -1;
+
+ close(fd);
+
+ return 0;
+}
+
/*
* This is a simple password generator
* return: On success, it will return the password, else NULL.
"!\"#$%&()*+,-./0123456789:;<=>?@ABCDE" \
"FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
int i;
+ unsigned short rand_nums[PASSWD_LEN];
+
+ if (generate_random_num(rand_nums, sizeof(rand_nums)) < 0) {
+ _E("Failed to read random data");
+ return NULL;
+ }
/* include null byte */
passwd = (char *)malloc(sizeof(char) * (PASSWD_LEN + 1));
}
for (i = 0; i < PASSWD_LEN; i++)
- passwd[i] = charset[g_random_int() % ASCII_PASSWD_CHAR];
+ passwd[i] = charset[rand_nums[i] % ASCII_PASSWD_CHAR];
passwd[i] = '\0';
return passwd;