Python 中这样生成加密货币钱包私钥的安全性是够的吗?

31 天前
 zictos

我觉得就这种方法最简单,据说随机性也很强,但是网上好像很多生成钱包的示例代码都不使用这种方法生成,所以不知道是否有更安全的生成方法。

import secrets
private_key = secrets.token_bytes(32)
private_key = private_key.hex()
print(private_key)
880 次点击
所在节点    Python
4 条回复
yzding
31 天前
tronpy 里面默认生成的密钥就是这样的,只不过大家都是从助记词生成 key
smallyu
31 天前
私钥的最大值是 fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140 ,而不是全 f 。所以这样有可能生成出来非法的钱包
zictos
31 天前
@yzding #1 我觉得助记词没什么用,更麻烦,64 个 16 进制字符是最简洁的。
@smallyu #2
好像有什么 ECDSA 算法和 secp256k1 曲线之类的概念,意思是只要生成出来的钱包是可用的,就肯定会满足这两个条件吗?还是说即便不满足这两个条件,钱包也依然可能是可用的,只是没那么安全吗?

网上的代码在生成时一般好像都会使用一些第三方模块:
比特币: bitcoin 、ecdsa 、binascii 等
以太坊:ethereum 、web3 、eth_utils 、eth_account 、eth_keys 等
ysc3839
31 天前
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.

所以一般不用担心,安全性是操作系统保证的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1043262

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX