summaryrefslogtreecommitdiff
path: root/rsa/pem.py
diff options
context:
space:
mode:
Diffstat (limited to 'rsa/pem.py')
-rw-r--r--rsa/pem.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/rsa/pem.py b/rsa/pem.py
index a06d59f..3f0ea76 100644
--- a/rsa/pem.py
+++ b/rsa/pem.py
@@ -58,3 +58,31 @@ def load_pem(contents, pem_start, pem_end):
pem = ''.join(pem_lines)
return base64.decodestring(pem)
+def save_pem(contents, pem_start, pem_end):
+ '''Saves a PEM file.
+
+ The PEM file will start with the 'pem_start' marker, then the
+ base64-encoded content, and end with the 'pem_end' marker.
+
+ @param contents: the contents to encode in PEM format
+ @param pem_start: the start marker of the PEM content, such as
+ '-----BEGIN RSA PRIVATE KEY-----'
+ @param pem_end: the end marker of the PEM content, such as
+ '-----END RSA PRIVATE KEY-----'
+
+ @return the base64-encoded content between the start and end markers.
+
+ '''
+
+ b64 = base64.encodestring(contents).strip()
+ pem_lines = [pem_start]
+
+ for block_start in range(0, len(b64), 64):
+ block = b64[block_start:block_start + 64]
+ pem_lines.append(block)
+
+ pem_lines.append(pem_end)
+ pem_lines.append('')
+
+ return '\n'.join(pem_lines)
+