diff options
| author | Christian Heimes <cheimes@redhat.com> | 2015-09-21 14:44:50 +0200 |
|---|---|---|
| committer | Christian Heimes <cheimes@redhat.com> | 2015-10-01 20:02:59 +0200 |
| commit | 344caf335d94e7aa3a32bdd850ed1363088c896d (patch) | |
| tree | 25407b09ad8a79e59f0446c47d3debc9b3adddfa /base/common/python/pki/encoder.py | |
| parent | 4f5051463ea9dc1366a2b58b9814c0e7997c1813 (diff) | |
| download | pki-344caf335d94e7aa3a32bdd850ed1363088c896d.tar.gz pki-344caf335d94e7aa3a32bdd850ed1363088c896d.tar.xz pki-344caf335d94e7aa3a32bdd850ed1363088c896d.zip | |
Replace legacy Python base64 invocations with Py3-safe code
Replace deprecated decodestring() and encodestring() with b64decode()
and b64encode().
Provice specialized encode_cert() / decode_cert() functions to handle
base64 encoding and decoding for X.509 certs in JSON strings. In Python
3 the base64 function don't suppor ASCII text, just ASCII bytes.
Diffstat (limited to 'base/common/python/pki/encoder.py')
| -rw-r--r-- | base/common/python/pki/encoder.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/base/common/python/pki/encoder.py b/base/common/python/pki/encoder.py index bf5d2e473..f83060103 100644 --- a/base/common/python/pki/encoder.py +++ b/base/common/python/pki/encoder.py @@ -1,11 +1,41 @@ from __future__ import absolute_import + +import base64 import json + +import six from six import iteritems, itervalues TYPES = {} NOTYPES = {} +def encode_cert(data): + """base64 encode X.509 certificate + + Python 3's base64.b64encode() doesn't support ASCII text. + + :param data: data as bytes or ASCII text + :type data: str, bytes + :rtype: bytes + """ + if isinstance(data, six.text_type): + data = data.encode('ascii') + return base64.b64encode(data) + + +def decode_cert(data): + """base64 decode X.509 certificate + + :param data: data as bytes or ASCII text + :type data: str, bytes + :rtype: bytes + """ + if isinstance(data, six.text_type): + data = data.encode('ascii') + return base64.b64decode(data) + + class CustomTypeEncoder(json.JSONEncoder): """ A custom JSONEncoder class that knows how to encode core custom |
