python密碼學(xué)庫(kù)pynacl功能介紹
python庫(kù)-密碼學(xué)庫(kù)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庫(kù)綁定封裝。PyNaCl是libsodium庫(kù)的Python實(shí)現(xiàn)。libsodium是一個(gè)基于NaCI開(kāi)發(fā)的先進(jìn)而且易用的加密庫(kù),主要用于加密、解密、簽名和生成密碼哈希等。PyNaCI能夠提供數(shù)字簽名、密鑰加密、公鑰加密、哈希和消息身份驗(yàn)證、基于密碼的密鑰派生和密碼散列功能。
libsodium 是c寫(xiě)的,現(xiàn)代,便攜式,易于使用的加密庫(kù)。Sodium是一個(gè)新的,易于使用的軟件庫(kù),用于加密,解密,簽名,密碼哈希等。
官網(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ù)字簽名允許您公布公共密鑰,然后您可以使用私有簽名密鑰來(lái)簽名消息。然后,擁有您的公鑰的其他人可以使用它來(lái)驗(yàn)證您的消息實(shí)際上是真實(shí)的。
簽名和驗(yàn)證消息而無(wú)需編碼密鑰或消息:
簽名 (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)
驗(yàn)簽 (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位)隨機(jī)種子值產(chǎn)生的。該值可以以32的長(zhǎng)度為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é)庫(kù)pynacl的文章就介紹到這了,更多相關(guān)python密碼學(xué)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)動(dòng)態(tài)生成系統(tǒng)數(shù)據(jù)庫(kù)設(shè)計(jì)到Word文檔
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)填寫(xiě)相關(guān)數(shù)據(jù)庫(kù)信息后,生成系統(tǒng)數(shù)據(jù)庫(kù)設(shè)計(jì)到word文檔,文中示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06Python實(shí)現(xiàn)隊(duì)列的方法示例小結(jié)【數(shù)組,鏈表】
這篇文章主要介紹了Python實(shí)現(xiàn)隊(duì)列的方法,結(jié)合實(shí)例形式分析了Python基于數(shù)組和鏈表實(shí)現(xiàn)隊(duì)列的相關(guān)操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02Python基于Socket實(shí)現(xiàn)簡(jiǎn)單聊天室
這篇文章主要為大家詳細(xì)介紹了Python基于Socket實(shí)現(xiàn)簡(jiǎn)單聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Python基于詞頻排序?qū)崿F(xiàn)快速挖掘關(guān)鍵詞
這篇文章主要為大家詳細(xì)介紹了Python如何基于詞頻排序?qū)崿F(xiàn)快速挖掘關(guān)鍵詞功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03Python利用正則表達(dá)式匹配并截取指定子串及去重的方法
這篇文章主要介紹了Python利用正則表達(dá)式匹配并截取指定子串及去重的方法,涉及Python正則表達(dá)式匹配及字符串截取操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Python無(wú)法安裝包的一種解決(Requirement already satisfied問(wèn)題)
這篇文章主要介紹了Python無(wú)法安裝包的一種解決(Requirement already satisfied問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(四)邏輯回歸
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)第四篇,邏輯回歸的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01