我觉得就这种方法最简单,据说随机性也很强,但是网上好像很多生成钱包的示例代码都不使用这种方法生成,所以不知道是否有更安全的生成方法。
import secrets
private_key = secrets.token_bytes(32)
private_key = private_key.hex()
print(private_key)
1
yzding 170 天前 via iPhone
tronpy 里面默认生成的密钥就是这样的,只不过大家都是从助记词生成 key
|
2
smallyu 170 天前
私钥的最大值是 fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140 ,而不是全 f 。所以这样有可能生成出来非法的钱包
|
3
zictos OP |
4
ysc3839 170 天前
https://docs.python.org/3/library/secrets.html
The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. https://github.com/python/cpython/blob/3.12/Lib/secrets.py from random import SystemRandom https://docs.python.org/3/library/random.html#random.SystemRandom Class that uses the os.urandom() function for generating random numbers from sources provided by the operating system. https://docs.python.org/3/library/os.html#os.urandom Return a bytestring of size random bytes suitable for cryptographic use. 所以一般不用担心,安全性是操作系统保证的。 |