[PATCH] Allow printing key digests in key edit

Christian Aistleitner christian at quelltextlich.at
Tue Jan 31 21:02:11 CET 2012


Hello,

on Mon, Jan 30, 2012 at 09:52:31AM -0500, Robert J. Hansen wrote:
> Then post your code as a diff against a 2.0.x tree and let interested
> users apply the patch themselves. 

So be it :)
Those interested in trying out / toying around with digests in the
2.0 series find a patch against the current STABLE-BRANCH-2-0 
attached below.

Kind regards,
Christian

---
 g10/keyedit.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 70 insertions(+), 1 deletions(-)

diff --git a/g10/keyedit.c b/g10/keyedit.c
index 76830f0..a9c358e 100644
--- a/g10/keyedit.c
+++ b/g10/keyedit.c
@@ -52,6 +52,8 @@ static void show_names(KBNODE keyblock,PKT_public_key *pk,
 static void show_key_with_all_names( KBNODE keyblock, int only_marked,
 	    int with_revoker, int with_fpr, int with_subkeys, int with_prefs );
 static void show_key_and_fingerprint( KBNODE keyblock );
+static void show_digest (KBNODE keyblock, int digest_algo);
+static int menu_digest (void);
 static int menu_adduid( KBNODE keyblock, KBNODE sec_keyblock,
 			int photo, const char *photo_name );
 static void menu_deluid( KBNODE pub_keyblock, KBNODE sec_keyblock );
@@ -1368,7 +1370,7 @@ enum cmdids
     cmdEXPIRE, cmdBACKSIGN, cmdENABLEKEY, cmdDISABLEKEY, cmdSHOWPREF,
     cmdSETPREF, cmdPREFKS, cmdNOTATION, cmdINVCMD, cmdSHOWPHOTO, cmdUPDTRUST,
     cmdCHKTRUST, cmdADDCARDKEY, cmdKEYTOCARD, cmdBKUPTOCARD, cmdCLEAN,
-    cmdMINIMIZE, cmdNOP
+    cmdMINIMIZE, cmdDIGEST, cmdNOP
   };
 
 static struct
@@ -1475,6 +1477,7 @@ static struct
       N_("compact unusable user IDs and remove unusable signatures from key")},
     { "minimize", cmdMINIMIZE  , KEYEDIT_NOT_SK,
       N_("compact unusable user IDs and remove all signatures from key") },
+    { "digest", cmdDIGEST, 0, N_("compute a digest for the key")},
     { NULL, cmdNONE, 0, NULL }
   };
 
@@ -1741,6 +1744,10 @@ keyedit_menu( const char *username, strlist_t locusr,
 	    show_key_and_fingerprint( keyblock );
 	    break;
 
+	case cmdDIGEST:
+	  show_digest( keyblock, menu_digest() );
+	  break;
+
 	  case cmdSELUID:
 	    if(strlen(arg_string)==NAMEHASH_LEN*2)
 	      redisplay=menu_select_uid_namehash(cur_keyblock,arg_string);
@@ -3040,6 +3047,68 @@ show_key_and_fingerprint( KBNODE keyblock )
     print_fingerprint( pk, NULL, 2 );
 }
 
+// Prints a digest of the public key in keyblock
+// using the digest_algo digest algorithm
+static void
+show_digest (KBNODE keyblock, int digest_algo)
+{
+  PKT_public_key *pk = NULL;
+  gcry_md_hd_t md;
+  const byte *dp;
+  int len;
+  int i;
+
+  assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
+  pk = keyblock->pkt->pkt.public_key;
+
+  tty_printf ("\n");
+
+  if (gcry_md_open (&md, digest_algo, 0))
+    BUG ();
+  hash_public_key(md,pk);
+  gcry_md_final( md );
+
+  dp = gcry_md_read( md, 0 );
+  len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
+  assert( len >=0 );
+  tty_printf("   %s digest of key = ", gcry_md_algo_name( digest_algo ) );
+  for ( i=0; i < len ; i++ )
+    tty_printf("%s%s%s%02X",
+	       (i&15)?"":"\n     ",
+	       (i&7)?"":" ",
+	       (i&1)?"":" ",
+	       dp[i] );
+  gcry_md_close( md );
+
+  tty_printf ("\n");
+}
+
+static int
+menu_digest ( void )
+{
+  int i;
+  char *answer;
+  int digest_algo = 0;
+
+  // Showing the possible digests
+  tty_printf (_("Please select what kind of digest you want:\n"));
+  for(i=0; i <= 110; i++ )
+    if( !openpgp_md_test_algo(i) )
+      {
+	tty_printf( "  (%d) %s\n", i, gcry_md_algo_name(i));
+      }
+
+  // User selects digest
+  while ( openpgp_md_test_algo( digest_algo ) )
+    {
+      answer = cpr_get( "keyedit.print_digest", _("Your selection? ") );
+      cpr_kill_prompt ();
+      digest_algo = *answer? atoi (answer) : DIGEST_ALGO_SHA512;
+    }
+
+  return digest_algo;
+}
+
 
 /* Show a warning if no uids on the key have the primary uid flag
    set. */
-- 
1.7.8.3

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: </pipermail/attachments/20120131/007997b2/attachment-0001.pgp>


More information about the Gnupg-devel mailing list