我正在尝试实现 Tor 的一些随机生成器.onion涉及生成 80 位数字以创建 16 字符散列的地址。
如何在 Python 中定义这样的变量?
"16-character hashes can be made up of any letter of the alphabet, and decimal digits beginning with 2 and ending with 7, thus representing an 80-bit number in base32."
链接:
请您参考如下方法:
如果你使用的是 Python3,你需要这个单行代码
import base64
import codecs
import random
data = base64.b32encode(
codecs.decode(codecs.encode(
'{0:020x}'.format(random.getrandbits(80))
), 'hex_codec')
)
解释:您使用 random.getrandbits 获取 80 个随机位,将它编码成二进制形式(你必须通过十六进制编码来完成这个过程,然后使用 base64.b32encode 函数,它提供了 RFC 3548 兼容的方法将其编码到你的 base32 目标编码中。
也适用于 Python 2。
