简介
在非对称加密(公/私钥)体系中,一般有两种应用场景:
- 公钥加密 ---> 私钥解密
- 私钥签名 ---> 公钥验证签名
数字签名在网络通信中是一种防伪和防抵赖技术。
RSA私钥签名及验证
- 加密算法:RSA
- 哈希算法:SHA256
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
data = b'hello,wolrd'
key = serialization.load_pem_private_key(key_pem)
sig = key.sign(data, padding=padding.PKCS1v15(), algorithm=hashes.SHA256())
椭圆曲线私钥签名及验证
- 加密算法:EC
- 哈希算法:SHA256
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa
data = b'hello,wolrd'
key = serialization.load_pem_private_key(key_pem)
sig = key.sign(data, ec.ECDSA(hashes.SHA256()))
国密签名及验证
- 加密算法:SM2
- 哈希算法:SM3
```python
from gm_crypto.sm2 import SM2PrivateKey
data = b'hello,wolrd'
key = SM2PrivateKey.load(key_pem)
sig = key.sign(data, hash_type='SM3')
标签:私钥,cryptography,方法,pem,签名,key,import,data
From: https://www.cnblogs.com/superhin/p/18311265/cryptography_sign_with_key