summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/x509.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/ipalib/x509.py b/ipalib/x509.py
index e67aab628..cac5e9c59 100644
--- a/ipalib/x509.py
+++ b/ipalib/x509.py
@@ -33,6 +33,7 @@
from __future__ import print_function
+import binascii
import collections
import os
import sys
@@ -552,6 +553,28 @@ def process_othernames(gns):
yield gn
+def chunk(size, s):
+ """Yield chunks of the specified size from the given string.
+
+ The input must be a multiple of the chunk size (otherwise
+ trailing characters are dropped).
+
+ Works on character strings only.
+
+ """
+ return (u''.join(span) for span in six.moves.zip(*[iter(s)] * size))
+
+
+def add_colons(s):
+ """Add colons between each nibble pair in a hex string."""
+ return u':'.join(chunk(2, s))
+
+
+def to_hex_with_colons(bs):
+ """Convert bytes to a hex string with colons."""
+ return add_colons(binascii.hexlify(bs).decode('utf-8'))
+
+
if __name__ == '__main__':
# this can be run with:
# python ipalib/x509.py < /etc/ipa/ca.crt