aboutsummaryrefslogtreecommitdiff
path: root/src/libnpass/libnpass.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnpass/libnpass.c')
-rw-r--r--src/libnpass/libnpass.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c
index 9158c62..25b707f 100644
--- a/src/libnpass/libnpass.c
+++ b/src/libnpass/libnpass.c
@@ -18,6 +18,17 @@
#define FPR_MAX 256
static char pass_dir[PATH_MAX] = {0};
+const char *pass_gen_set[] = {
+ "0123456789",
+
+ "0123456789"
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+
+ "0123456789"
+ "abcdefghijklmnopqrstuvwxyz"
+ "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
+};
int set_pass_dir(void);
@@ -123,6 +134,31 @@ ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream)
return r;
}
+int pass_gen(pass_gen_t gen, char *pass, size_t n)
+{
+ int r;
+ FILE *rand;
+ size_t pass_gen_len;
+
+
+ if (n <= 0)
+ err_ret(1, "small password");
+
+ rand = fopen("/dev/urandom", "r");
+ if (!rand)
+ err_ret(1, "%s", strerror(errno));
+
+ r = fread(pass, sizeof(pass[0]), n, rand);
+ if (r != (int) n)
+ err_ret(1, "fread failed");
+
+ pass_gen_len = strlen(pass_gen_set[gen]);
+ for (size_t i = 0; i < n; i++)
+ pass[i] = pass_gen_set[gen][pass[i] % (pass_gen_len - 1)];
+
+ return 0;
+}
+
int pass_add(const char *path, const char *pass, size_t n)
{
int r;