[PATCH 1/3] unify formatting of fingerprints

Stefan Tomanek tomanek at internet-sicherheit.de
Fri Sep 13 08:58:19 CEST 2013


This change introduces a common function to turn
a fingerprint into a human readable format.

Signed-off-by: Stefan Tomanek <tomanek at internet-sicherheit.de>
---
 g10/keylist.c |   57 +++++++++++++++++++++++++++++----------------------------
 g10/main.h    |    1 +
 2 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/g10/keylist.c b/g10/keylist.c
index 6618a7f..4426efd 100644
--- a/g10/keylist.c
+++ b/g10/keylist.c
@@ -1459,6 +1459,28 @@ list_keyblock( KBNODE keyblock, int secret, int fpr, void *opaque )
         list_keyblock_print (keyblock, secret, fpr, opaque );
 }
 
+char *
+format_fingerprint(const byte *fpr, const int len)
+{
+    int str_len =     len*2                  /* 2 hex digits per byte */
+                  +  (len*2-1) / 4           /* one space every 4 digits */
+                  + ((len*2-1) / 4 - 1) / 5; /* one additional space after 5 groups */
+    char *fp = xmalloc_clear(str_len + 1);
+
+    int i;
+    int j;
+    for (i = 0, j = 0; i < len && j < str_len-1; i++, j+=2)
+      {
+        if (i && i%2 == 0)
+          fp[j++] = ' ';
+        if (len == 20 && i == 10)
+          fp[j++] = ' ';
+        sprintf(&fp[j], "%02X", fpr[i]);
+      }
+    fp[str_len] = '\0';
+    return fp;
+}
+
 /*
  * standard function to print the finperprint.
  * mode 0: as used in key listings, opt.with_colons is honored
@@ -1555,34 +1577,13 @@ print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode )
             fputs (text, fp);
         else
             tty_printf ("%s", text);
-	if (n == 20) {
-	    for (i=0; i < n ; i++, i++, p += 2 ) {
-                if (fp) {
-                    if (i == 10 )
-                        putc(' ', fp);
-                    fprintf (fp, " %02X%02X", *p, p[1] );
-                }
-                else {
-                    if (i == 10 )
-                        tty_printf (" ");
-                    tty_printf (" %02X%02X", *p, p[1]);
-                }
-	    }
-	}
-	else {
-	    for (i=0; i < n ; i++, p++ ) {
-                if (fp) {
-                    if (i && !(i%8) )
-                        putc (' ', fp);
-                    fprintf (fp, " %02X", *p );
-                }
-                else {
-                    if (i && !(i%8) )
-                        tty_printf (" ");
-                    tty_printf (" %02X", *p );
-                }
-	    }
-	}
+
+        char *fpr = format_fingerprint(p, n);
+        if (fp)
+            fprintf (fp, " %s", fpr);
+        else
+            tty_printf (" %s", fpr);
+        xfree(fpr);
     }
     if (fp)
         putc ('\n', fp);
diff --git a/g10/main.h b/g10/main.h
index 784ade0..eadac1f 100644
--- a/g10/main.h
+++ b/g10/main.h
@@ -250,6 +250,7 @@ void secret_key_list( STRLIST list );
 void print_subpackets_colon(PKT_signature *sig);
 void reorder_keyblock (KBNODE keyblock);
 void list_keyblock( KBNODE keyblock, int secret, int fpr, void *opaque );
+char *format_fingerprint(const byte *fpr, const int len);
 void print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode);
 void print_revokers(PKT_public_key *pk);
 void show_policy_url(PKT_signature *sig,int indent,int mode);
-- 
1.7.10.4



More information about the Gnupg-devel mailing list