aboutsummaryrefslogtreecommitdiff
path: root/src/libnpass/libnpass.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-04-07 12:55:01 +0530
committersinanmohd <sinan@sinanmohd.com>2024-04-07 12:56:31 +0530
commite500ce0e53cb059dae35016abd8e649c2ba6f0f2 (patch)
tree0445c3d3a49772fbfa26ed42f2cea53f7e1673be /src/libnpass/libnpass.c
parent8ab1e3c5da78c05b8edf48d44e6fde046d50e582 (diff)
pass/gen: init
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;