summaryrefslogtreecommitdiff
path: root/rsa/key.py
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2016-01-27 15:43:28 +0100
committerSybren A. Stüvel <sybren@stuvel.eu>2016-01-27 15:43:28 +0100
commit4a6a2b7b7facd5bcbf8b9e137d0047a170c5e8d4 (patch)
treef09bf80dcfea9c875cf605413f308a196ee9a139 /rsa/key.py
parent417ec0d7081ddcd94e31be2741d5b63600a3fe53 (diff)
downloadrsa-git-4a6a2b7b7facd5bcbf8b9e137d0047a170c5e8d4.tar.gz
Removed code duplication
Diffstat (limited to 'rsa/key.py')
-rw-r--r--rsa/key.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/rsa/key.py b/rsa/key.py
index c1931fc..7bea9d2 100644
--- a/rsa/key.py
+++ b/rsa/key.py
@@ -70,14 +70,21 @@ class AbstractKey(object):
'DER': cls._load_pkcs1_der,
}
- if format not in methods:
+ method = cls._assert_format_exists(format, methods)
+ return method(keyfile)
+
+ @staticmethod
+ def _assert_format_exists(file_format, methods):
+ """Checks whether the given file format exists in 'methods'.
+ """
+
+ try:
+ return methods[file_format]
+ except KeyError:
formats = ', '.join(sorted(methods.keys()))
- raise ValueError('Unsupported format: %r, try one of %s' % (format,
+ raise ValueError('Unsupported format: %r, try one of %s' % (file_format,
formats))
- method = methods[format]
- return method(keyfile)
-
def save_pkcs1(self, format='PEM'):
"""Saves the public key in PKCS#1 DER or PEM format.
@@ -90,12 +97,7 @@ class AbstractKey(object):
'DER': self._save_pkcs1_der,
}
- if format not in methods:
- formats = ', '.join(sorted(methods.keys()))
- raise ValueError('Unsupported format: %r, try one of %s' % (format,
- formats))
-
- method = methods[format]
+ method = self._assert_format_exists(format, methods)
return method()
def blind(self, message, r):