本文整理汇总了Python中rsa.core.decrypt_int方法的典型用法代码示例。如果您正苦于以下问题:Python core.decrypt_int方法的具体用法?Python core.decrypt_int怎么用?Python core.decrypt_int使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rsa.core
的用法示例。
在下文中一共展示了core.decrypt_int方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_raw_hash
# 需要导入模块: from rsa import core [as 别名]
# 或者: from rsa.core import decrypt_int [as 别名]
def extract_raw_hash(signature, pub_key, is_sha256):
hash_size = SHA256_HASH_SIZE if is_sha256 else SHA1_HASH_SIZE
keylength = common.byte_size(pub_key.n)
encrypted = transform.bytes2int(signature)
decrypted = core.decrypt_int(encrypted, pub_key.e, pub_key.n)
clearsig = transform.int2bytes(decrypted, keylength)
# unpad
if (clearsig[0] != '\x00' or clearsig[1] != '\x01'):
raise Exception('Invalid signature format')
null_idx = clearsig.find('\x00', 2)
if null_idx < 0:
raise Exception('Invalid signature format')
padding = clearsig[2:null_idx]
if len(padding) != keylength - 2 - 1 - hash_size:
raise Exception('Invalid signature format')
if not all(p == '\xff' for p in padding):
raise Exception('Invalid signature format')
raw_hash = clearsig[null_idx + 1:]
if len(raw_hash) != hash_size:
raise Exception('Invalid signature format.')
return raw_hash
示例2: verify
# 需要导入模块: from rsa import core [as 别名]
# 或者: from rsa.core import decrypt_int [as 别名]
def verify(message, signature, pub_key):
"""Verifies that the signature matches the message.
The hash method is detected automatically from the signature.
:param message: the signed message. Can be an 8-bit string or a file-like
object. If ``message`` has a ``read()`` method, it is assumed to be a
file-like object.
:param signature: the signature block, as created with :py:func:`rsa.sign`.
:param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message.
:raise VerificationError: when the signature doesn't match the message.
"""
keylength = common.byte_size(pub_key.n)
encrypted = transform.bytes2int(signature)
decrypted = core.decrypt_int(encrypted, pub_key.e, pub_key.n)
clearsig = transform.int2bytes(decrypted, keylength)
# Get the hash method
method_name = _find_method_hash(clearsig)
message_hash = _hash(message, method_name)
# Reconstruct the expected padded hash
cleartext = HASH_ASN1[method_name] + message_hash
expected = _pad_for_signing(cleartext, keylength)
# Compare with the signed one
if expected != clearsig:
raise VerificationError('Verification failed')
return True
示例3: verify
# 需要导入模块: from rsa import core [as 别名]
# 或者: from rsa.core import decrypt_int [as 别名]
def verify(message, signature, pub_key):
'''Verifies that the signature matches the message.
The hash method is detected automatically from the signature.
:param message: the signed message. Can be an 8-bit string or a file-like
object. If ``message`` has a ``read()`` method, it is assumed to be a
file-like object.
:param signature: the signature block, as created with :py:func:`rsa.sign`.
:param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message.
:raise VerificationError: when the signature doesn't match the message.
'''
keylength = common.byte_size(pub_key.n)
encrypted = transform.bytes2int(signature)
decrypted = core.decrypt_int(encrypted, pub_key.e, pub_key.n)
clearsig = transform.int2bytes(decrypted, keylength)
# Get the hash method
method_name = _find_method_hash(clearsig)
message_hash = _hash(message, method_name)
# Reconstruct the expected padded hash
cleartext = HASH_ASN1[method_name] + message_hash
expected = _pad_for_signing(cleartext, keylength)
# Compare with the signed one
if expected != clearsig:
raise VerificationError('Verification failed')
return True
示例4: verify
# 需要导入模块: from rsa import core [as 别名]
# 或者: from rsa.core import decrypt_int [as 别名]
def verify(message, signature, pub_key):
'''Verifies that the signature matches the message.
The hash method is detected automatically from the signature.
:param message: the signed message. Can be an 8-bit string or a file-like
object. If ``message`` has a ``read()`` method, it is assumed to be a
file-like object.
:param signature: the signature block, as created with :py:func:`rsa.sign`.
:param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message.
:raise VerificationError: when the signature doesn't match the message.
.. warning::
Never display the stack trace of a
:py:class:`rsa.pkcs1.VerificationError` exception. It shows where in
the code the exception occurred, and thus leaks information about the
key. It's only a tiny bit of information, but every bit makes cracking
the keys easier.
'''
blocksize = common.byte_size(pub_key.n)
encrypted = transform.bytes2int(signature)
decrypted = core.decrypt_int(encrypted, pub_key.e, pub_key.n)
clearsig = transform.int2bytes(decrypted, blocksize)
# If we can't find the signature marker, verification failed.
if clearsig[0:2] != b('\x00\x01'):
raise VerificationError('Verification failed')
# Find the 00 separator between the padding and the payload
try:
sep_idx = clearsig.index(b('\x00'), 2)
except ValueError:
raise VerificationError('Verification failed')
# Get the hash and the hash method
(method_name, signature_hash) = _find_method_hash(clearsig[sep_idx+1:])
message_hash = _hash(message, method_name)
# Compare the real hash to the hash in the signature
if message_hash != signature_hash:
raise VerificationError('Verification failed')
return True