V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zictos
V2EX  ›  Python

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

  •  
  •   zictos · 23 天前 · 798 次点击

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

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

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

    所以一般不用担心,安全性是操作系统保证的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2367 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:00 · PVG 12:00 · LAX 21:00 · JFK 00:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.