[seahorse] pgp-subkey: Prettify fingerprints in a smarter way



commit c4d316335c0adc04c1e0f321ed2598079ac6f8e3
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Apr 9 17:28:41 2014 -0500

    pgp-subkey: Prettify fingerprints in a smarter way
    
    seahorse_pgp_subkey_calc_fingerprint() assumed that it would only take
    strings composed of hex digits and nothing else, and it would group
    digits in chunks of four.  Now the function can take strings composed
    of hex digits and whitespace, and it will format them in chunks of
    four digits with a space between them.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=719872

 pgp/seahorse-pgp-subkey.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/pgp/seahorse-pgp-subkey.c b/pgp/seahorse-pgp-subkey.c
index 4b45981..826723e 100644
--- a/pgp/seahorse-pgp-subkey.c
+++ b/pgp/seahorse-pgp-subkey.c
@@ -369,12 +369,16 @@ seahorse_pgp_subkey_calc_description (const gchar *name, guint index)
        return g_strdup_printf (_("Subkey %d of %s"), index, name);
 }
 
+/* Takes runs of hexadecimal digits, possibly with whitespace among them, and
+ * formats them nicely in groups of four digits.
+ */
 gchar*
 seahorse_pgp_subkey_calc_fingerprint (const gchar *raw_fingerprint)
 {
        const gchar *raw;
        GString *string;
-       guint index, len;
+       guint i, len;
+       guint num_digits;
        gchar *fpr;
            
        raw = raw_fingerprint;
@@ -382,15 +386,22 @@ seahorse_pgp_subkey_calc_fingerprint (const gchar *raw_fingerprint)
 
        string = g_string_new ("");
        len = strlen (raw);
-           
-       for (index = 0; index < len; index++) {
-               if (index > 0 && index % 4 == 0)
-                       g_string_append (string, " ");
-               g_string_append_c (string, raw[index]);
+
+       num_digits = 0;
+       for (i = 0; i < len; i++) {
+               if (g_ascii_isxdigit (raw[i])) {
+                       g_string_append_c (string, g_ascii_toupper (raw[i]));
+                       num_digits++;
+
+                       if (num_digits > 0 && num_digits % 4 == 0)
+                               g_string_append (string, " ");
+               }
        }
-           
+
        fpr = string->str;
        g_string_free (string, FALSE);
-           
+
+       g_strchomp (fpr);
+
        return fpr;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]