Skip to content

Commit fedcaa1

Browse files
committed
Ensuring that PEM output is always in bytes.
This may break some applications. However, it does make the RSA library easier to use on different Python versions.
1 parent 83a8110 commit fedcaa1

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

rsa/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def keygen():
8383
outfile.write(data)
8484
else:
8585
print('Writing private key to stdout', file=sys.stderr)
86+
if sys.version_info[0] >= 3:
87+
data = data.decode('ascii') # on Py3 we must write text, not bytes
8688
sys.stdout.write(data)
8789

8890

@@ -189,6 +191,8 @@ def write_outfile(self, outdata, outname):
189191
outfile.write(outdata)
190192
else:
191193
print('Writing output to stdout', file=sys.stderr)
194+
if sys.version_info[0] >= 3:
195+
data = outdata.decode('ascii') # on Py3 we must write text, not bytes
192196
sys.stdout.write(outdata)
193197

194198

rsa/pem.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
def _markers(pem_marker):
2424
"""
25-
Returns the start and end PEM markers
25+
Returns the start and end PEM markers, as bytes.
2626
"""
2727

28-
if is_bytes(pem_marker):
29-
pem_marker = pem_marker.decode('utf-8')
28+
if not is_bytes(pem_marker):
29+
pem_marker = pem_marker.encode('ascii')
3030

31-
return (b('-----BEGIN %s-----' % pem_marker),
32-
b('-----END %s-----' % pem_marker))
31+
return (b('-----BEGIN ') + pem_marker + b('-----'),
32+
b('-----END ') + pem_marker + b('-----'))
3333

3434

3535
def load_pem(contents, pem_marker):
@@ -106,7 +106,7 @@ def save_pem(contents, pem_marker):
106106
when your file has '-----BEGIN RSA PRIVATE KEY-----' and
107107
'-----END RSA PRIVATE KEY-----' markers.
108108
109-
:return: the base64-encoded content between the start and end markers.
109+
:return: the base64-encoded content between the start and end markers, as bytes.
110110
111111
"""
112112

tests/test_pem.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import unittest
1919

20-
from rsa._compat import b
20+
from rsa._compat import b, is_bytes
2121
from rsa.pem import _markers
2222
import rsa.key
2323

@@ -72,3 +72,17 @@ def test_bytes_private(self):
7272
key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
7373
self.assertEqual(prime1, key.p)
7474
self.assertEqual(prime2, key.q)
75+
76+
77+
class TestByteOutput(unittest.TestCase):
78+
"""Tests that PEM and DER are returned as bytes."""
79+
80+
def test_bytes_public(self):
81+
key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
82+
self.assertTrue(is_bytes(key.save_pkcs1(format='DER')))
83+
self.assertTrue(is_bytes(key.save_pkcs1(format='PEM')))
84+
85+
def test_bytes_private(self):
86+
key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
87+
self.assertTrue(is_bytes(key.save_pkcs1(format='DER')))
88+
self.assertTrue(is_bytes(key.save_pkcs1(format='PEM')))

0 commit comments

Comments
 (0)