之前讲到RSA可以用来加密和数字签名,这里是RSA用作数字签名。
Python的pycrypto库实现的数字签名有一个限制,必须对哈希(hash)值进行签名,而不能直接对原文进行数字签名。好像大部分实现都有此限制。
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import Crypto.Hash.SHA512
def rsa_sign(plaintext, key, hash_algorithm=Crypto.Hash.SHA512):
"""RSA 数字签名"""
signer = PKCS1_v1_5.new(RSA.importKey(key))
#hash算法必须要pycrypto库里的hash算法,不能直接用系统hashlib库,pycrypto是封装的hashlib
hash_value = hash_algorithm.new(plaintext)
return signer.sign(hash_value)
def rsa_verify(sign, plaintext, key, hash_algorithm=Crypto.Hash.SHA512):
"""校验RSA 数字签名"""
hash_value = hash_algorithm.new(plaintext)
verifier = PKCS1_v1_5.new(RSA.importKey(key))
return verifier.verify(hash_value, sign)
if __name__ == &#

本文介绍了如何使用Python的pycrypto库实现RSA数字签名。由于pycrypto库的限制,需要先对原文进行哈希运算,然后对哈希值进行签名。文中提供了一个完整的签名和验证示例。
最低0.47元/天 解锁文章

790

被折叠的 条评论
为什么被折叠?



