什么更随机,hashlib或urandom? [英] Whats more random, hashlib or urandom?

查看:103
本文介绍了什么更随机,hashlib或urandom?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一位朋友一起工作,我们需要生成一个随机散列。在我们有时间讨论之前,我们都提出了不同的方法,并且因为他们使用了不同的模块,所以我想问问大家什么会更好 - 如果有这样的事情。

  hashlib.sha1(str(random.random())).hexdigest()


$ b

  os.urandom(16).encode('hex')

输入这个问题让我想到第二种方法更好。简单胜于复杂。如果你同意,这对于随机生成哈希有多可靠?我如何测试这个?

解决方案

此解决方案:

  os.urandom(16).encode('hex')

一个href =http://docs.python.org/library/os.html#os.urandom>使用操作系统来产生随机性,该随机性可用于加密目的( )取决于操作系统的实现)。



random.random()生成pseudo-随机值

散列一个随机值不会增加任何新的随机性。

p>

I'm working on a project with a friend where we need to generate a random hash. Before we had time to discuss, we both came up with different approaches and because they are using different modules, I wanted to ask you all what would be better--if there is such a thing.

hashlib.sha1(str(random.random())).hexdigest()

or

os.urandom(16).encode('hex')

Typing this question out has got me thinking that the second method is better. Simple is better than complex. If you agree, how reliable is this for 'randomly' generating hashes? How would I test this?

解决方案

This solution:

os.urandom(16).encode('hex')

is the best since it uses the OS to generate randomness which should be usable for cryptographic purposes (depends on the OS implementation).

random.random() generates pseudo-random values.

Hashing a random value does not add any new randomness.

这篇关于什么更随机,hashlib或urandom?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆