Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b75a228

Browse files
authored
bpo-31659: Use simple slicing to format PEM cert (GH-3849)
DER_cert_to_PEM_cert() used textwrap.fill() to format PEM. But it's library to wrap lines on word boundary, while PEM is base64 encoded string. Additionally, importing textwrap is little slow.
1 parent edc05c5 commit b75a228

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

Lib/ssl.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"""
9292

9393
import ipaddress
94-
import textwrap
9594
import re
9695
import sys
9796
import os
@@ -1201,9 +1200,10 @@ def DER_cert_to_PEM_cert(der_cert_bytes):
12011200
PEM version of it as a string."""
12021201

12031202
f = str(base64.standard_b64encode(der_cert_bytes), 'ASCII', 'strict')
1204-
return (PEM_HEADER + '\n' +
1205-
textwrap.fill(f, 64) + '\n' +
1206-
PEM_FOOTER + '\n')
1203+
ss = [PEM_HEADER]
1204+
ss += [f[i:i+64] for i in range(0, len(f), 64)]
1205+
ss.append(PEM_FOOTER + '\n')
1206+
return '\n'.join(ss)
12071207

12081208
def PEM_cert_to_DER_cert(pem_cert_string):
12091209
"""Takes a certificate in ASCII PEM format and returns the

0 commit comments

Comments
 (0)