diff options
Diffstat (limited to 'rsa/pem.py')
-rw-r--r-- | rsa/pem.py | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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) + |