哪种随机方法更好?

时间:2018-03-13 14:30:21

标签: random cryptography key python-3.6 bitcoin

pybitcointools(https://github.com/primal100/pybitcointools)使用以下函数生成比特币私钥(在main.py: https://github.com/primal100/pybitcointools/blob/master/cryptos/main.py中):

import hashlib
def random_key():
    entropy = random_string(32) \
        + str(random.randrange(2**256)) \
        + str(int(time.time() * 1000000))
    return sha256(entropy)

在其他地方我看到了一个更简单的实现:

import os
os.urandom(32).hex()

我想知道哪个更随机,哪个更安全?

1 个答案:

答案 0 :(得分:2)

os.urandom()使用专门为加密安全随机数设计的系统函数,远远优于您展示的其他函数。第一个功能看起来非常像某人"滚动你自己的"一个随机字符串的版本,虽然他没有完全搞砸它,但它不足以支持加密。

永远不要自己加密!

相关问题