python密碼學(xué)庫pynacl功能介紹
python庫-密碼學(xué)庫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實現(xiàn)。libsodium是一個基于NaCI開發(fā)的先進而且易用的加密庫,主要用于加密、解密、簽名和生成密碼哈希等。PyNaCI能夠提供數(shù)字簽名、密鑰加密、公鑰加密、哈希和消息身份驗證、基于密碼的密鑰派生和密碼散列功能。
libsodium 是c寫的,現(xiàn)代,便攜式,易于使用的加密庫。Sodium是一個新的,易于使用的軟件庫,用于加密,解密,簽名,密碼哈希等。
官網(wǎng):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
數(shù)字簽名使用example
官方:https://pynacl.readthedocs.io/en/latest/signing/
數(shù)字簽名允許您公布公共密鑰,然后您可以使用私有簽名密鑰來簽名消息。然后,擁有您的公鑰的其他人可以使用它來驗證您的消息實際上是真實的。
簽名和驗證消息而無需編碼密鑰或消息:
簽名 (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算法生產(chǎn)數(shù)字簽名的私鑰。
簽名密鑰是由32字節(jié)(256位)隨機種子值產(chǎn)生的。該值可以以32的長度為bytes()傳遞到簽名密鑰中。
參數(shù):
seed (bytes) – Random 32-byte value (i.e. private key).
encoder – A class that is able to decode the seed.
到此這篇關(guān)于python密碼學(xué)庫pynacl的文章就介紹到這了,更多相關(guān)python密碼學(xué)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python?Matplotlib處理地理數(shù)據(jù)可視化
地理數(shù)據(jù)可視化是數(shù)據(jù)科學(xué)中一個重要的領(lǐng)域,它幫助我們理解和分析與地理位置相關(guān)的數(shù)據(jù),Python?提供了強大的工具來處理地理數(shù)據(jù),本文將介紹如何使用?Python?Matplotlib?處理地理數(shù)據(jù)可視化,包括基礎(chǔ)概念、常用庫、數(shù)據(jù)處理以及實際案例,需要的朋友可以參考下2024-11-11
使用python把xmind轉(zhuǎn)換成excel測試用例的實現(xiàn)代碼
這篇文章主要介紹了使用python把xmind轉(zhuǎn)換成excel測試用例的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
python批量插入數(shù)據(jù)到mysql的3種方法
這篇文章主要給大家介紹了關(guān)于python批量插入數(shù)據(jù)到mysql的3種方法,在日常處理數(shù)據(jù)的過程中,我們都有批量寫入數(shù)據(jù)庫的需求,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10

