@@ -33,6 +33,8 @@ fn main() -> Result<(), Error> {
3333 Some ( "import" ) => import ( ) ,
3434 Some ( "upgrade" ) => upgrade ( ) ,
3535 Some ( "print" ) => print_public_key ( ) ,
36+ Some ( "fingerprint" ) => print_fingerprint ( ) ,
37+ Some ( "key-id" ) => print_key_id ( ) ,
3638 Some ( "--help" ) => print_help_message ( ) ,
3739 Some ( arg) if gpg_sign_arg ( arg) => verify_commit ( ) ,
3840 _ => {
@@ -54,7 +56,10 @@ fn print_help_message() -> Result<(), Error> {
5456 println ! ( "A program for signing git commits.\n " ) ;
5557 println ! ( "Arguments:" ) ;
5658 println ! ( " init <userid>: Generate a keypair and store in the keychain." ) ;
57- println ! ( " print: Print public key in OpenPGP format.\n " ) ;
59+ println ! ( " import <key>: Import a key from the command line." ) ;
60+ println ! ( " print: Print public key in OpenPGP format." ) ;
61+ println ! ( " fingerprint: Print the fingerprint of the public key." ) ;
62+ println ! ( " key-id: Print the key ID of the public key.\n " ) ;
5863 println ! ( "See https://github.com/pkgxdev/bpb for more information." ) ;
5964 Ok ( ( ) )
6065}
@@ -104,6 +109,29 @@ fn print_public_key() -> Result<(), Error> {
104109 Ok ( ( ) )
105110}
106111
112+ fn get_fingerprint ( ) -> Result < pbp:: Fingerprint , Error > {
113+ let config = Config :: load ( ) ?;
114+ let service = config. service ( ) ;
115+ let account = config. user_id ( ) ;
116+ let secret_str = get_keychain_item ( service, account) ?;
117+ let secret = to_32_bytes ( & secret_str) ?;
118+
119+ let keypair = KeyData :: load ( & config, secret) ?;
120+ Ok ( keypair. fingerprint ( ) )
121+ }
122+
123+ // Prints the fingerprint (sha256 hash of the public key -- 20 bytes)
124+ fn print_fingerprint ( ) -> Result < ( ) , Error > {
125+ println ! ( "{}" , pretty_print_hex_string( & get_fingerprint( ) ?) ) ;
126+ Ok ( ( ) )
127+ }
128+
129+ // Prints the long key ID (the last 8 bytes of the fingerprint)
130+ fn print_key_id ( ) -> Result < ( ) , Error > {
131+ println ! ( "{}" , pretty_print_hex_string( & get_fingerprint( ) ?[ 12 ..] ) ) ;
132+ Ok ( ( ) )
133+ }
134+
107135fn verify_commit ( ) -> Result < ( ) , Error > {
108136 use std:: io:: Read ;
109137
@@ -123,7 +151,7 @@ fn verify_commit() -> Result<(), Error> {
123151 let sig = keypair. sign ( commit. as_bytes ( ) ) ?;
124152
125153 eprintln ! ( "\n [GNUPG:] SIG_CREATED " ) ;
126- println ! ( "{}" , sig ) ;
154+ println ! ( "{sig}" ) ;
127155 Ok ( ( ) )
128156}
129157
@@ -167,3 +195,11 @@ fn to_32_bytes(slice: &String) -> Result<[u8; 32], Error> {
167195 array[ ..len] . copy_from_slice ( & vector[ ..len] ) ;
168196 Ok ( array)
169197}
198+
199+ // iterates over a hex array and prints space-separated groups of four characters
200+ fn pretty_print_hex_string ( hex : & [ u8 ] ) -> String {
201+ hex. chunks ( 2 )
202+ . map ( hex:: encode_upper)
203+ . collect :: < Vec < String > > ( )
204+ . join ( " " )
205+ }
0 commit comments