summaryrefslogtreecommitdiff
path: root/obnamlib/encryption.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-03-27 12:45:24 +0100
committerLars Wirzenius <liw@liw.fi>2011-03-27 12:45:24 +0100
commitf4b6df3ec38a428e4320139c681e3b2e08cab347 (patch)
treedcce72f15117569b9411de20a5871986d1c7f775 /obnamlib/encryption.py
parent53a615f0bb6d01a2b05c37aac3686a5e00ed8f81 (diff)
downloadobnam-f4b6df3ec38a428e4320139c681e3b2e08cab347.tar.gz
Add obnamlib.get_public_key.
Diffstat (limited to 'obnamlib/encryption.py')
-rw-r--r--obnamlib/encryption.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/obnamlib/encryption.py b/obnamlib/encryption.py
index 6de56013..7ddc9760 100644
--- a/obnamlib/encryption.py
+++ b/obnamlib/encryption.py
@@ -89,3 +89,28 @@ def decrypt_with_symmetric_key(encrypted, key):
'''Decrypt encrypted data with symmetric encryption.'''
return _gpg_pipe(['-d'], encrypted, key)
+
+def _gpg(args, gpghome=None):
+ '''Run gpg and return its output.'''
+
+ env = dict()
+ env.update(os.environ)
+ if gpghome is not None:
+ env['GNUPGHOME'] = gpghome
+
+ argv = ['gpg', '-q', '--batch'] + args
+ p = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, env=env)
+ out, err = p.communicate('')
+
+ # Return output data, or deal with errors.
+ if p.returncode: # pragma: no cover
+ raise Exception(err)
+
+ return out
+
+
+def get_public_key(keyid, gpghome=None):
+ '''Return the ASCII armored export form of a given public key.'''
+ return _gpg(['--export', '--armor', keyid], gpghome=gpghome)
+