diff options
author | Sybren A. St?vel <sybren@stuvel.eu> | 2011-07-21 22:04:39 +0200 |
---|---|---|
committer | Sybren A. St?vel <sybren@stuvel.eu> | 2011-07-21 22:04:39 +0200 |
commit | 68584a0901f33624e1b34a24cee25f9b7514f673 (patch) | |
tree | 1b0108acd5fbc30658fe7965621cb803e68e137c /rsa | |
parent | 90085efa4c5acb3ebdb862157c9ecf10aafa6530 (diff) | |
download | rsa-68584a0901f33624e1b34a24cee25f9b7514f673.tar.gz |
Simpler PEM marker code
Diffstat (limited to 'rsa')
-rw-r--r-- | rsa/key.py | 7 | ||||
-rw-r--r-- | rsa/pem.py | 40 |
2 files changed, 25 insertions, 22 deletions
@@ -12,9 +12,6 @@ of pyasn1. import rsa.prime import rsa.pem -PEM_PRIVATE_KEY_START = '-----BEGIN RSA PRIVATE KEY-----' -PEM_PRIVATE_KEY_END = '-----END RSA PRIVATE KEY-----' - class PublicKey(object): '''Represents a public RSA key. @@ -339,7 +336,7 @@ def load_private_key_pem(keyfile): @return: a PrivateKey object ''' - der = rsa.pem.load_pem(keyfile, PEM_PRIVATE_KEY_START, PEM_PRIVATE_KEY_END) + der = rsa.pem.load_pem(keyfile, 'RSA PRIVATE KEY') return load_private_key_der(der) def save_private_key_pem(priv_key): @@ -350,7 +347,7 @@ def save_private_key_pem(priv_key): ''' der = save_private_key_der(priv_key) - return rsa.pem.save_pem(der, PEM_PRIVATE_KEY_START, PEM_PRIVATE_KEY_END) + return rsa.pem.save_pem(der, 'RSA PRIVATE KEY') __all__ = ['PublicKey', 'PrivateKey', 'newkeys', 'load'] @@ -2,18 +2,24 @@ import base64 -def load_pem(contents, pem_start, pem_end): - '''Loads a PEM file. +def _markers(pem_marker): + '''Returns the start and end PEM markers + + >>> _markers('RSA PRIVATE KEY') + ('-----BEGIN RSA PRIVATE KEY-----', '-----END RSA PRIVATE KEY-----') - Only considers the information between lines "pem_start" and "pem_end". For - private keys these are '-----BEGIN RSA PRIVATE KEY-----' and - '-----END RSA PRIVATE KEY-----' + ''' + + return ('-----BEGIN %s-----' % pem_marker, + '-----END %s-----' % pem_marker) + +def load_pem(contents, pem_marker): + '''Loads a PEM file. @param contents: the contents of the file to interpret - @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-----' + @param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY' + when your file has '-----BEGIN RSA PRIVATE KEY-----' and + '-----END RSA PRIVATE KEY-----' markers. @return the base64-decoded content between the start and end markers. @@ -22,6 +28,8 @@ def load_pem(contents, pem_start, pem_end): ''' + (pem_start, pem_end) = _markers(pem_marker) + pem_lines = [] in_pem_part = False @@ -58,22 +66,20 @@ def load_pem(contents, pem_start, pem_end): pem = ''.join(pem_lines) return base64.decodestring(pem) -def save_pem(contents, pem_start, pem_end): +def save_pem(contents, pem_marker): '''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-----' + @param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY' + when your file has '-----BEGIN RSA PRIVATE KEY-----' and + '-----END RSA PRIVATE KEY-----' markers. @return the base64-encoded content between the start and end markers. ''' + (pem_start, pem_end) = _markers(pem_marker) + b64 = base64.encodestring(contents).strip() pem_lines = [pem_start] |