diff options
author | Nicholas Tay <nick@windblume.net> | 2021-12-02 18:05:28 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2021-12-02 18:06:43 +1100 |
commit | 2b9eeca7b61c78e34293462c820099a3086aebaf (patch) | |
tree | 8c4e5b81ecf4c49283821e69baa59dc5e2aff0d2 | |
parent | 01004c96c024bb091934e39588cdc73552c32335 (diff) | |
download | passgen-2b9eeca7b61c78e34293462c820099a3086aebaf.tar.gz passgen-2b9eeca7b61c78e34293462c820099a3086aebaf.tar.bz2 passgen-2b9eeca7b61c78e34293462c820099a3086aebaf.zip |
Add better RNG for Linux
-rw-r--r-- | passgen.c | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -3,9 +3,14 @@ #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
+#include <string.h>
+
+#ifdef __linux__
+#include <sys/random.h>
+#else
#include <time.h>
#include <unistd.h>
-#include <string.h>
+#endif
#define CLASS(c, chars) \
case c: \
@@ -51,8 +56,14 @@ int main(int argc, char *argv[]) char password[grammar_size+1];
password[grammar_size] = 0;
- // seed RNG; this isn't very good, but it's enough (for now)
+#ifdef __linux__
+ // Reduce syscalls by generating a bunch and then doing it
+ unsigned int entropy[grammar_size];
+ getrandom(&entropy, sizeof(entropy), 0);
+#else
+ // seed RNG; this isn't very good, but it's enough(?)
srand(time(NULL) + getpid() % 420 - 69);
+#endif
for (int i = 0; i < grammar_size; ++i) {
char c = grammar[i];
@@ -75,7 +86,12 @@ int main(int argc, char *argv[]) }
do {
- password[i] = class[rand() % class_size] - (caps ? 'a' - 'A' : 0);
+#ifdef __linux__
+ unsigned int r = entropy[i];
+#else
+ long r = rand();
+#endif
+ password[i] = class[r % class_size] - (caps ? 'a' - 'A' : 0);
} while (i != 0 && password[i] == password[i-1]);
}
|