Always pad fingerprints to 16 characters
[project/usign.git] / main.c
diff --git a/main.c b/main.c
index 2203d0185611f6b4a9b13358f47c9c81cbabc7d0..ebfdfb0809c4d8394b2254625d47649975d72000 100644 (file)
--- a/main.c
+++ b/main.c
@@ -129,6 +129,7 @@ get_file(const char *filename, char *buf, int buflen)
 
        len = fread(buf, 1, buflen - 1, f);
        buf[len] = 0;
+       fclose(f);
 }
 
 static bool
@@ -148,7 +149,7 @@ static void write_file(const char *name, const uint8_t *fingerprint,
        if (comment)
                fputs(comment, f);
        else
-               fprintf(f, "%s %"PRIx64, prefix,
+               fprintf(f, "%s %016"PRIx64, prefix,
                        fingerprint_u64(fingerprint));
        fprintf(f, "\n%s\n", buf);
        fclose(f);
@@ -171,11 +172,12 @@ static int verify(const char *msgfile)
        if (!get_base64_file(sigfile, &sig, sizeof(sig), buf, sizeof(buf)) ||
            memcmp(sig.pkalg, "Ed", 2) != 0) {
                fprintf(stderr, "Failed to decode signature\n");
+               fclose(f);
                return 1;
        }
 
        if (!pubkeyfile) {
-               snprintf(buf, sizeof(buf), "%s/%"PRIx64, pubkeydir,
+               snprintf(buf, sizeof(buf), "%s/%016"PRIx64, pubkeydir,
                         fingerprint_u64(sig.fingerprint));
                pubkeyfile = buf;
        }
@@ -183,6 +185,7 @@ static int verify(const char *msgfile)
        if (!get_base64_file(pubkeyfile, &pkey, sizeof(pkey), buf, sizeof(buf)) ||
            memcmp(pkey.pkalg, "Ed", 2) != 0) {
                fprintf(stderr, "Failed to decode public key\n");
+               fclose(f);
                return 1;
        }
 
@@ -273,7 +276,7 @@ static int fingerprint(void)
        else
                return 1;
 
-       fprintf(stdout, "%"PRIx64"\n", fingerprint_u64(fp));
+       fprintf(stdout, "%016"PRIx64"\n", fingerprint_u64(fp));
        return 0;
 }
 
@@ -292,11 +295,16 @@ static int generate(void)
        FILE *f;
 
        f = fopen("/dev/urandom", "r");
-       if (!f ||
-           fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
+       if (!f) {
+               fprintf(stderr, "Can't open /dev/urandom\n");
+               return 1;
+       }
+
+       if (fread(skey.fingerprint, sizeof(skey.fingerprint), 1, f) != 1 ||
            fread(skey.seckey, EDSIGN_SECRET_KEY_SIZE, 1, f) != 1 ||
            fread(skey.salt, sizeof(skey.salt), 1, f) != 1) {
                fprintf(stderr, "Can't read data from /dev/urandom\n");
+               fclose(f);
                return 1;
        }
        if (f)
@@ -333,7 +341,7 @@ static int usage(const char *cmd)
                "  -V:                  verify (needs at least -m and -p|-P)\n"
                "  -S:                  sign (needs at least -m and -s)\n"
                "  -F:                  print key fingerprint of public/secret key or signature\n"
-               "  -G:                  generate a new keypair\n"
+               "  -G:                  generate a new keypair (needs at least -p and -s)\n"
                "Options:\n"
                "  -c <comment>:        add comment to keys\n"
                "  -m <file>:           message file\n"