imgtool: Add public key's SHA256 hash calculation

Signed-off-by: Dávid Házi <david.hazi@arm.com>
Signed-off-by: Bence Balogh <bence.balogh@arm.com>
Change-Id: I91d5c07c1bb2b8abe2592cd49b2053c881465ba2
diff --git a/scripts/imgtool/main.py b/scripts/imgtool/main.py
index 896afdd..c3fce54 100755
--- a/scripts/imgtool/main.py
+++ b/scripts/imgtool/main.py
@@ -61,6 +61,7 @@
 
 
 valid_langs = ['c', 'rust']
+valid_hash_encodings = ['lang-c', 'raw']
 valid_encodings = ['lang-c', 'lang-rust', 'pem', 'raw']
 keygens = {
     'rsa-2048':   gen_rsa2048,
@@ -158,6 +159,34 @@
         raise click.UsageError()
 
 
+@click.option('-e', '--encoding', metavar='encoding',
+              type=click.Choice(valid_hash_encodings),
+              help='Valid encodings: {}. '
+                   'Default value is {}.'
+                   .format(', '.join(valid_hash_encodings),
+                           valid_hash_encodings[0]))
+@click.option('-k', '--key', metavar='filename', required=True)
+@click.option('-o', '--output', metavar='output', required=False,
+              help='Specify the output file\'s name. \
+                    The stdout is used if it is not provided.')
+@click.command(help='Dump the SHA256 hash of the public key')
+def getpubhash(key, output, encoding):
+    if not encoding:
+        encoding = valid_hash_encodings[0]
+    key = load_key(key)
+
+    if not output:
+        output = sys.stdout
+    if key is None:
+        print("Invalid passphrase")
+    elif encoding == 'lang-c':
+        key.emit_c_public_hash(file=output)
+    elif encoding == 'raw':
+        key.emit_raw_public_hash(file=output)
+    else:
+        raise click.UsageError()
+
+
 @click.option('--minimal', default=False, is_flag=True,
               help='Reduce the size of the dumped private key to include only '
                    'the minimum amount of data required to decrypt. This '
@@ -486,6 +515,7 @@
 
 imgtool.add_command(keygen)
 imgtool.add_command(getpub)
+imgtool.add_command(getpubhash)
 imgtool.add_command(getpriv)
 imgtool.add_command(verify)
 imgtool.add_command(sign)