[elbe-devel] [PATCH v2 6/9] egpg: Implement unarmor_openpgp_keyring

Holger Dengler holger at hdengler.de
Mon Mar 6 12:57:38 CET 2023


On 06/03/2023 12:47, Bastian Germann wrote:
> Add a function to convert an ascii-armored OpenPGP key to its binary
> representation.
> 
> Signed-off-by: Bastian Germann <bage at linutronix.de>
> ---
>  elbepack/egpg.py | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/elbepack/egpg.py b/elbepack/egpg.py
> index 653d02d856..36e85e3f61 100644
> --- a/elbepack/egpg.py
> +++ b/elbepack/egpg.py
> @@ -5,6 +5,8 @@
>  #
>  # SPDX-License-Identifier: GPL-3.0-or-later
>  
> +import binascii

Is this still required? I think not...

> +import logging
>  import os
>  
>  from gpg import core
> @@ -12,7 +14,7 @@ from gpg.constants import sigsum, sig, PROTOCOL_OpenPGP
>  from gpg.errors import GPGMEError, KeyNotFound, InvalidSigners
>  
>  from elbepack.filesystem import hostfs
> -from elbepack.shellhelper import system
> +from elbepack.shellhelper import command_out_stderr, system
>  
>  elbe_internal_key_param = """
>  <GnupgKeyParms format="internal">
> @@ -272,3 +274,15 @@ def generate_elbe_internal_key():
>  def export_key(fingerprint, outfile):
>      system(f"/usr/bin/gpg -a -o {outfile} --export {fingerprint}",
>             env_add={"GNUPGHOME": "/var/cache/elbe/gnupg"})
> +
> +def unarmor_openpgp_keyring(armored):
> +    """
> +    Unarmors one ascii-armored (string) OpenPGP keyring.
> +    Returns a binary string (empty for invalid keys).
> +    """
> +    conv_cmd = command_out_stderr('/usr/bin/gpg --no-options --dearmor', stdin=armored)
> +    if conv_cmd[0]:
> +        logging.error(conv_cmd[2])
> +        return b""
> +
> +    return conv_cmd[1]

-- 
Gruß,
Holger Dengler
--
holger at hdengler.de


More information about the elbe-devel mailing list