網站首頁 編程語言 正文
python庫-密碼學庫pynacl
什么是pynacl
官方: https://pynacl.readthedocs.io/en/latest/
PyNaCl is a Python binding to libsodium, which is a fork of the Networking and Cryptography library. These libraries have a stated goal of improving usability, security and speed. It supports Python 3.6+ as well as PyPy 3.
PyNaCl 是 libsodium C庫綁定封裝。PyNaCl是libsodium庫的Python實現。libsodium是一個基于NaCI開發的先進而且易用的加密庫,主要用于加密、解密、簽名和生成密碼哈希等。PyNaCI能夠提供數字簽名、密鑰加密、公鑰加密、哈希和消息身份驗證、基于密碼的密鑰派生和密碼散列功能。
libsodium 是c寫的,現代,便攜式,易于使用的加密庫。Sodium是一個新的,易于使用的軟件庫,用于加密,解密,簽名,密碼哈希等。
官網:libsodium.org
github: https://github.com/jedisct1/libsodium
PyNaCl功能:
- Digital signatures
- Secret-key encryption
- Public-key encryption
- Hashing and message authentication
- Password based key derivation and password hashing
數字簽名使用example
官方:https://pynacl.readthedocs.io/en/latest/signing/
數字簽名允許您公布公共密鑰,然后您可以使用私有簽名密鑰來簽名消息。然后,擁有您的公鑰的其他人可以使用它來驗證您的消息實際上是真實的。
簽名和驗證消息而無需編碼密鑰或消息:
簽名 (SigningKey):
from nacl.encoding import Base64Encoder
from nacl.signing import SigningKey
# Generate a new random signing key
signing_key = SigningKey.generate()
# Sign a message with the signing key
signed_b64 = signing_key.sign(b"Attack at Dawn", encoder=Base64Encoder)
# Obtain the verify key for a given signing key
verify_key = signing_key.verify_key
# Serialize the verify key to send it to a third party
verify_key_b64 = verify_key.encode(encoder=Base64Encoder)
驗簽 (VerifyKey):
from nacl.encoding import Base64Encoder
from nacl.signing import VerifyKey
# Create a VerifyKey object from a base64 serialized public key
verify_key = VerifyKey(verify_key_b64, encoder=Base64Encoder)
# Check the validity of a message's signature
# The message and the signature can either be passed together, or
# separately if the signature is decoded to raw bytes.
# These are equivalent:
verify_key.verify(signed_b64, encoder=Base64Encoder)
signature_bytes = Base64Encoder.decode(signed_b64.signature)
verify_key.verify(signed_b64.message, signature_bytes,
encoder=Base64Encoder)
# Alter the signed message text
forged = signed_b64[:-1] + bytes([int(signed_b64[-1]) ^ 1])
# Will raise nacl.exceptions.BadSignatureError, since the signature check
# is failing
verify_key.verify(forged)
Traceback (most recent call last):
...
nacl.exceptions.BadSignatureError: Signature was forged or corrupt
classnacl.signing.SigningKey(seed, encoder)[source]?
使用ED25519算法生產數字簽名的私鑰。
簽名密鑰是由32字節(256位)隨機種子值產生的。該值可以以32的長度為bytes()傳遞到簽名密鑰中。
參數:
seed (bytes) – Random 32-byte value (i.e. private key).
encoder – A class that is able to decode the seed.
原文鏈接:https://blog.csdn.net/inthat/article/details/124550307
相關推薦
- 2022-05-29 C++重載運算符你真的了解嗎_C 語言
- 2023-07-04 maven引入本地jar,打包問題
- 2022-08-28 Redis唯一ID生成器的實現_Redis
- 2022-07-19 分布式事務其中的那些坑
- 2022-10-23 Python繪制牛奶凍曲線(高木曲線)案例_python
- 2023-01-19 GO的基礎知識掃盲注意事項_Golang
- 2022-09-02 selenium動態數據獲取的方法實現_python
- 2022-08-18 python數據可視化pygal模擬擲骰子實現示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支