noobsheldon
V2EX  ›  Python

werkzeug 中 serving 中的一段代码,注释好像有亮点。。。。

By noobsheldon at 2017 年 1 月 19 日 · 2502 次点击
def generate_adhoc_ssl_pair(cn=None):
from random import random
crypto = _get_openssl_crypto_module()

# pretty damn sure that this is not actually accepted by anyone
if cn is None:
cn = '*'

cert = crypto.X509()
cert.set_serial_number(int(random() * sys.maxsize))
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(60 * 60 * 24 * 365)

subject = cert.get_subject()
subject.CN = cn
subject.O = 'Dummy Certificate'

issuer = cert.get_issuer()
issuer.CN = 'Untrusted Authority'
issuer.O = 'Self-Signed'

pkey = crypto.PKey()
pkey.generate_key(crypto.TYPE_RSA, 2048)
cert.set_pubkey(pkey)
cert.sign(pkey, 'sha256')

return cert, pkey
3 条回复  •  2017-01-19 14:37:47 +08:00
sylecn
   1
sylecn  
   2017 年 1 月 19 日 via Android
你理解有问题吧。证书里面 cn 字段和中国没有半毛钱关系。网站用的证书 cn 需要设置为域名或者泛域名。

这里测试证书直接把 cn 字段默认值设置成*,作者希望不会有人去信任它。仅此而已。
wwqgtxx
   2
wwqgtxx  
   2017 年 1 月 19 日
CN (Common Name)
不要自己臆想
noobsheldon
   3
noobsheldon  
OP
   2017 年 1 月 19 日 via Android
@wwqgtxx
@sylecn 受教了!
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 30ms · 3.9.8.5