python密码学库pynacl功能介绍

作者:西京刀客 时间:2021-03-07 01:29:44 

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

标签:python,密码学,库,pynacl
0
投稿

猜你喜欢

  • 在CentOS 6 中安装WordPress(一) 安装Apache,Mysql, PHP环境

    2023-11-06 17:41:24
  • PHP simplexml_load_file()函数讲解

    2023-06-03 23:16:56
  • ASP实例:使用ASP生成图片彩色校验码

    2009-01-20 16:27:00
  • IE7的web标准之道 Ⅱ

    2008-08-13 12:50:00
  • HTTP中header头部信息详解

    2023-06-11 23:33:17
  • python 爬虫基本使用——统计杭电oj题目正确率并排序

    2021-11-25 17:16:35
  • 如何在python中使用selenium的示例

    2023-07-15 20:52:09
  • python矩阵运算,转置,逆运算,共轭矩阵实例

    2021-09-16 17:45:22
  • 针对SQL Server中业务规则链接的分析

    2009-01-20 11:43:00
  • python中startswith()和endswith()的用法详解

    2023-11-02 12:41:09
  • 利用PHP自动生成印有用户信息的名片

    2023-09-10 22:30:56
  • Python中利用原始套接字进行网络编程的示例

    2023-06-22 18:02:03
  • 使用Python的Bottle框架写一个简单的服务接口的示例

    2023-09-01 17:29:50
  • Python实现备份文件实例

    2022-01-03 11:10:05
  • 内联格式化模式(line-height原理)

    2008-06-29 14:37:00
  • python实现字典(dict)和字符串(string)的相互转换方法

    2021-10-19 18:22:44
  • 页面新开窗口的一点补充

    2008-09-10 12:57:00
  • CI框架整合smarty步骤详解

    2023-11-14 11:18:11
  • js字符串日期yyyy-MM-dd转化为date示例代码

    2023-08-06 16:35:58
  • Python3如何在服务器打印资产信息

    2023-11-15 08:03:45
  • asp之家 网络编程 m.aspxhome.com