| 1 | #include "common.h" |
| 2 | #include "genpass.h" |
| 3 | |
| 4 | void genpass(int count, bool issecure) { |
| 5 | char *lang = getlang(); |
| 6 | |
| 7 | const char *charset_risky = |
| 8 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 9 | const char *charset_secure = |
| 10 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; |
| 11 | const char *charset = issecure ? charset_secure : charset_risky; |
| 12 | |
| 13 | FILE *fp = fopen("/dev/random", "rb"); |
| 14 | if (fp == NULL) { |
| 15 | if (strncmp(lang, "en", 2) == 0) |
| 16 | perror("Could not open /dev/random"); |
| 17 | else perror("/dev/randomを開けられませんでした"); |
| 18 | exit(EXIT_FAILURE); |
| 19 | } |
| 20 | |
| 21 | char password[count + 1]; |
| 22 | for (int i = 0; i < count; i++) { |
| 23 | unsigned char key; |
| 24 | fread(&key, sizeof(key), 1, fp); |
| 25 | password[i] = charset[key % strlen(charset)]; |
| 26 | } |
| 27 | |
| 28 | password[count] = '\0'; |
| 29 | fclose(fp); |
| 30 | |
| 31 | printf("%s\n", password); |
| 32 | } |