Python中文
首页
教程
问答
标签
搜索
登录
注册
python SystemRandom/os.urandom总是有足够的熵来实现好的cryp吗
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个密码生成器:</p> <pre><code>import random, string def gen_pass(): foo = random.SystemRandom() length = 64 chars = string.letters + string.digits return ''.join(foo.choice(chars) for _ in xrange(length)) </code></pre> <p>根据文档,SystemRandom使用os.urandom,它使用/dev/urandom抛出随机cryto位。在Linux中,您可以从/dev/urandom或/dev/random中获取随机位,它们都使用内核可以使用的任何熵。可用的熵量可以用tail/proc/sys/kernel/random/entropy-avail进行检查,这将返回一个类似于:129的数字。数值越高,可用的熵就越多。/dev/urandom和/dev/random的区别在于,只有当熵可用性足够高(至少60)时,/dev/random才会吐出位,/dev/urandom总是吐出位。文档中说/dev/urandom适合加密,您只需要对ssl证书等使用/dev/random。</p> <p>我的问题是,genúpass是否有助于始终生成强加密级密码?如果我尽快调用这个函数,我会不会因为熵池耗尽而在某个时刻停止得到强cryto比特?</p> <p>问题还可能是为什么/dev/urandom<em>总是</em>产生强cryto位<em>而</em>不关心熵的效用?</p> <p>有可能是/dev/urandom的设计使其带宽被你能猜到的循环次数所限制,这将与熵量相关,但这是推测,我找不到答案。</p> <p>这也是我的第一个stackoverflow问题,请批评我。我担心,当知道答案的人可能知道背景时,我给了很多背景。</p> <p>谢谢</p> <p><strong>更新</strong></p> <p>当从中读取<code>/dev/urandom</code>时,我编写了一些代码来查看熵池:</p> <pre><code>import subprocess import time from pygooglechart import Chart from pygooglechart import SimpleLineChart from pygooglechart import Axis def check_entropy(): arg = ['cat', '/proc/sys/kernel/random/entropy_avail'] ps = subprocess.Popen(arg,stdout=subprocess.PIPE) return int(ps.communicate()[0]) def run(number_of_tests,resolution,entropy = []): i = 0 while i < number_of_tests: time.sleep(resolution) entropy += [check_entropy()] i += 1 graph(entropy,int(number_of_tests*resolution)) def graph(entropy,rng): max_y = 200 chart = SimpleLineChart(600, 375, y_range=[0, max_y]) chart.add_data(entropy) chart.set_colours(['0000FF']) left_axis = range(0, max_y + 1, 32) left_axis[0] = 'entropy' chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM,['time in second']+get_x_axis(rng)) chart.download('line-stripes.png') def get_x_axis(rng): global modnum if len(filter(lambda x:x%modnum == 0,range(rng + 1)[1:])) > 10: modnum += 1 return get_x_axis(rng) return filter(lambda x:x%modnum == 0,range(rng + 1)[1:]) modnum = 1 run(500,.1) </code></pre> <p>如果运行此命令并同时运行:</p> <pre><code>while 1 > 0: gen_pass() </code></pre> <p>然后我很可靠地得到了一个这样的图表: <img src="https://i.stack.imgur.com/ev7OS.png" alt="enter image description here"/></p> <p>在运行<code>cat /dev/urandom</code>时生成图形看起来更可笑,并且<code>cat /dev/random</code>会变得毫无意义,并且很快保持低位(这也只是每3秒左右读取一个字节)</p> <p><strong>更新</strong></p> <p>如果我运行相同的测试,但使用了6个gen_pass()实例,则会得到以下结果: <img src="https://i.stack.imgur.com/yRHpS.png" alt="enter image description here"/></p> <p>所以看起来好像有什么东西使我有足够的熵。我应该测量密码生成率,并确保它实际上是被封顶的,因为如果不是,那么可能会发生一些可疑的事情。</p> <p><strong>更新</strong></p> <p>我找到这个<a href="http://lists.openwall.net/linux-kernel/2007/12/04/257" rel="noreferrer">email chain</a></p> <p>这表明,一旦池中只有128位,天王星将停止拉熵。这与上述结果非常一致,意味着在这些测试中,我经常生成垃圾密码。</p> <p>我以前的假设是,如果熵效用足够高(比如64位以上),那么<code>/dev/urnadom</code>输出是好的。不是这样的,似乎<code>/dev/urandom</code>是为了给<code>/dev/random</code>留下额外的熵,以备需要。</p> <p>现在我需要找出一个系统随机调用需要多少真正的随机位。</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您可能想了解一下为什么/dev/urandom是解决问题的方法:</p> <p><a href="http://www.2uo.de/myths-about-urandom/" rel="nofollow noreferrer">http://www.2uo.de/myths-about-urandom/</a></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
VirtualEnvRapper错误:路径python2(来自python=python2)不存在
10 回答
virtualenvs上的pyinstaller,没有名为导入错误的模块
6 回答
virtualenvs是否可以退回到用户包而不是系统包?
2 回答
virtualenvwrapper CentOS7
1 回答
virtualenvwrapper IOError:[Errno 13]权限被拒绝
4 回答
virtualenvwrapper mkproject和shell在windows中的启动问题?
1 回答
virtualenvwrapper mkvirtualenv不工作但没有错误
4 回答
Virtualenvwrapper python bash
3 回答
virtualenvwrapper:“workon”何时更改到项目目录?
7 回答
virtualenvwrapper:mkvirtualenv可以工作,但是rmvirtualenv返回bash:没有这样的文件或目录
7 回答
virtualenvwrapper:virtualenv信息存储在哪里?
8 回答
virtualenvwrapper:命令“python设置.pyegg_info“失败,错误代码为1
8 回答
virtualenvwrapper:如何将mkvirtualenv的默认Python版本/路径更改为ins
6 回答
Virtualenvwrapper:模块“pkg_resources”没有属性“iter_entry_points”
5 回答
Virtualenvwrapper:没有名为virtualenvwrapp的模块
8 回答
Virtualenvwrapper.bash_profi的正确设置
6 回答
Virtualenvwrapper.hook:权限被拒绝
4 回答
virtualenvwrapper.sh:fork:资源暂时不可用Python/Djang
10 回答
Virtualenvwrapper.shlssitepackages命令不工作
3 回答
Virtualenvwrapper.sh函数在bash sh中不可用
3 回答