diff options
author | Jim Shaver <dcypherd@gmail.com> | 2015-04-29 23:11:48 -0400 |
---|---|---|
committer | Jim Shaver <dcypherd@gmail.com> | 2015-04-29 23:11:48 -0400 |
commit | aab9dddb60efaeb2c6cc6f20a7e9bda4a393721a (patch) | |
tree | 674720e59f774f81b0883ba8e56925252281cacc /examples | |
parent | 473fe6ac0453c5f2f8ade81a8af8b670238c9b86 (diff) | |
download | pyopenssl-git-aab9dddb60efaeb2c6cc6f20a7e9bda4a393721a.tar.gz |
convert cert and private key creation to use context managers
Diffstat (limited to 'examples')
-rw-r--r-- | examples/mk_simple_certs.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/examples/mk_simple_certs.py b/examples/mk_simple_certs.py index 7a24a4f..84429f4 100644 --- a/examples/mk_simple_certs.py +++ b/examples/mk_simple_certs.py @@ -9,9 +9,11 @@ careq = createCertRequest(cakey, CN='Certificate Authority') cacert = createCertificate(careq, (careq, cakey), 0, (0, 60*60*24*365*5)) # five years print('Creating Certificate Authority private key in "simple/CA.pkey"') -open('simple/CA.pkey', 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, cakey).decode('utf-8')) +with open('simple/CA.pkey', 'w') as capkey: + capkey.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, cakey).decode('utf-8')) print('Creating Certificate Authority certificate in "simple/CA.cert"') -open('simple/CA.cert', 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cacert).decode('utf-8')) +with open('simple/CA.cert', 'w') as ca: + ca.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cacert).decode('utf-8')) for (fname, cname) in [('client', 'Simple Client'), ('server', 'Simple Server')]: pkey = createKeyPair(TYPE_RSA, 2048) @@ -19,6 +21,9 @@ for (fname, cname) in [('client', 'Simple Client'), ('server', 'Simple Server')] cert = createCertificate(req, (cacert, cakey), 1, (0, 60*60*24*365*5)) # five years print('Creating Certificate %s private key in "simple/%s.pkey"' % (fname, fname)) - open('simple/%s.pkey' % (fname,), 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey).decode('utf-8')) + with open('simple/%s.pkey' % (fname,), 'w') as leafpkey: + leafpkey.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey).decode('utf-8')) print('Creating Certificate %s certificate in "simple/%s.cert"' % (fname, fname)) - open('simple/%s.cert' % (fname,), 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8')) + with open('simple/%s.cert' % (fname,), 'w') as leafcert: + leafcert.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8')) + |