Skip to content

Commit 6ab5251

Browse files
committed
Simpler PEM marker code
1 parent f6a1073 commit 6ab5251

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

rsa/key.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import rsa.prime
1313
import rsa.pem
1414

15-
PEM_PRIVATE_KEY_START = '-----BEGIN RSA PRIVATE KEY-----'
16-
PEM_PRIVATE_KEY_END = '-----END RSA PRIVATE KEY-----'
17-
1815

1916
class PublicKey(object):
2017
'''Represents a public RSA key.
@@ -339,7 +336,7 @@ def load_private_key_pem(keyfile):
339336
@return: a PrivateKey object
340337
'''
341338

342-
der = rsa.pem.load_pem(keyfile, PEM_PRIVATE_KEY_START, PEM_PRIVATE_KEY_END)
339+
der = rsa.pem.load_pem(keyfile, 'RSA PRIVATE KEY')
343340
return load_private_key_der(der)
344341

345342
def save_private_key_pem(priv_key):
@@ -350,7 +347,7 @@ def save_private_key_pem(priv_key):
350347
'''
351348

352349
der = save_private_key_der(priv_key)
353-
return rsa.pem.save_pem(der, PEM_PRIVATE_KEY_START, PEM_PRIVATE_KEY_END)
350+
return rsa.pem.save_pem(der, 'RSA PRIVATE KEY')
354351

355352

356353
__all__ = ['PublicKey', 'PrivateKey', 'newkeys', 'load']

rsa/pem.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
import 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

Comments
 (0)