1+1=10

扬长避短 vs 取长补短

Python与加解密库

接前面Qt加解密库,整理一下Python中的状况

Python标准库

  • https://docs.python.org/3/library/crypto.html

Python标准库中有以下几个和crypto相关:

  • hashlib:哈希和消息摘要
  • hmac:密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code)。RFC2104
  • secrets:随机数
  • crypt:Unix下密码的哈希相关。在Python3.11中已经废弃,3.13中将删除。

第三方库

pycryptodome

  • https://pycryptodome.readthedocs.io
  • https://github.com/Legrandin/pycryptodome

pycryptodome 是 PyCrypto 的一个fork。PyCrypto 2013年后就停止更新了。

pycryptodome不是其他C库的封装,使用python编写。

安装有两个包名字可选,取决于:

要直接替代PyCrypto

pip install pycryptodome

还是要与PyCrypto共存

pip install pycryptodomex

cryptography

  • https://cryptography.io
  • https://github.com/pyca/cryptography

依赖于OpenSSL的C运行库。

cryptography大致分为两个层次:

  • 高层级recipe:对称加密 和 X509相关。
  • 底层级接口:包括对称加密、消息摘要、非对称加密、key生成等

安装方式

pip install cryptography

PyNaCL

  • https://pynacl.readthedocs.io/en/latest/
  • https://github.com/pyca/pynacl

和cryptography一样,它也在pyca之下。PyNaCI是 libsodium的python绑定。libsodium是NaCL的一个fork。

NaCL是 Networking and Cryptography library 首字符缩写。

pyOpenSSL

  • https://pypi.org/project/pyOpenSSL/

不建议用于ssl之外场合

M2Crypto

  • https://gitlab.com/m2crypto/m2crypto/

对OpenSSL的封装。在python 1.x阶段就已经存在。自身文档似乎很少

rsa

  • https://stuvel.eu/software/rsa/
  • https://github.com/sybrenstuvel/python-rsa

纯python实现

其他

  • pycrypto:https://pypi.org/project/pycrypto/ 2013年后没有更新
  • crypto:https://pypi.org/project/crypto/ 2015年没有更新,不支持Windows

参考

  • https://docs.python.org/3/library/crypto.html
  • https://datatracker.ietf.org/doc/html/rfc2104.html
  • https://pycryptodome.readthedocs.io/en/latest/src/examples.html
  • https://nacl.cr.yp.to/
  • https://doc.libsodium.org
  • https://gist.github.com/atoponce/07d8d4c833873be2f68c34f9afc5a78a

Comments