22
33import base64
44
5- def load_pem (contents , pem_start , pem_end ):
6- '''Loads a PEM file.
5+ def _markers (pem_marker ):
6+ '''Returns the start and end PEM markers
7+
8+ >>> _markers('RSA PRIVATE KEY')
9+ ('-----BEGIN RSA PRIVATE KEY-----', '-----END RSA PRIVATE KEY-----')
710
8- Only considers the information between lines "pem_start" and "pem_end". For
9- private keys these are '-----BEGIN RSA PRIVATE KEY-----' and
10- '-----END RSA PRIVATE KEY-----'
11+ '''
12+
13+ return ('-----BEGIN %s-----' % pem_marker ,
14+ '-----END %s-----' % pem_marker )
15+
16+ def load_pem (contents , pem_marker ):
17+ '''Loads a PEM file.
1118
1219 @param contents: the contents of the file to interpret
13- @param pem_start: the start marker of the PEM content, such as
14- '-----BEGIN RSA PRIVATE KEY-----'
15- @param pem_end: the end marker of the PEM content, such as
16- '-----END RSA PRIVATE KEY-----'
20+ @param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
21+ when your file has '-----BEGIN RSA PRIVATE KEY-----' and
22+ '-----END RSA PRIVATE KEY-----' markers.
1723
1824 @return the base64-decoded content between the start and end markers.
1925
@@ -22,6 +28,8 @@ def load_pem(contents, pem_start, pem_end):
2228
2329 '''
2430
31+ (pem_start , pem_end ) = _markers (pem_marker )
32+
2533 pem_lines = []
2634 in_pem_part = False
2735
@@ -58,22 +66,20 @@ def load_pem(contents, pem_start, pem_end):
5866 pem = '' .join (pem_lines )
5967 return base64 .decodestring (pem )
6068
61- def save_pem (contents , pem_start , pem_end ):
69+ def save_pem (contents , pem_marker ):
6270 '''Saves a PEM file.
6371
64- The PEM file will start with the 'pem_start' marker, then the
65- base64-encoded content, and end with the 'pem_end' marker.
66-
6772 @param contents: the contents to encode in PEM format
68- @param pem_start: the start marker of the PEM content, such as
69- '-----BEGIN RSA PRIVATE KEY-----'
70- @param pem_end: the end marker of the PEM content, such as
71- '-----END RSA PRIVATE KEY-----'
73+ @param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
74+ when your file has '-----BEGIN RSA PRIVATE KEY-----' and
75+ '-----END RSA PRIVATE KEY-----' markers.
7276
7377 @return the base64-encoded content between the start and end markers.
7478
7579 '''
7680
81+ (pem_start , pem_end ) = _markers (pem_marker )
82+
7783 b64 = base64 .encodestring (contents ).strip ()
7884 pem_lines = [pem_start ]
7985
0 commit comments