How to use the rsa.common.byte_size function in rsa

To help you get started, we’ve selected a few rsa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sybrenstuvel / python-rsa / tests / test_common.py View on Github external
def test_values(self):
        self.assertEqual(byte_size(1 << 1023), 128)
        self.assertEqual(byte_size((1 << 1024) - 1), 128)
        self.assertEqual(byte_size(1 << 1024), 129)
        self.assertEqual(byte_size(255), 1)
        self.assertEqual(byte_size(256), 2)
        self.assertEqual(byte_size(0xffff), 2)
        self.assertEqual(byte_size(0xffffff), 3)
        self.assertEqual(byte_size(0xffffffff), 4)
        self.assertEqual(byte_size(0xffffffffff), 5)
        self.assertEqual(byte_size(0xffffffffffff), 6)
        self.assertEqual(byte_size(0xffffffffffffff), 7)
        self.assertEqual(byte_size(0xffffffffffffffff), 8)
github sybrenstuvel / python-rsa / tests / test_common.py View on Github external
def test_values(self):
        self.assertEqual(byte_size(1 << 1023), 128)
        self.assertEqual(byte_size((1 << 1024) - 1), 128)
        self.assertEqual(byte_size(1 << 1024), 129)
        self.assertEqual(byte_size(255), 1)
        self.assertEqual(byte_size(256), 2)
        self.assertEqual(byte_size(0xffff), 2)
        self.assertEqual(byte_size(0xffffff), 3)
        self.assertEqual(byte_size(0xffffffff), 4)
        self.assertEqual(byte_size(0xffffffffff), 5)
        self.assertEqual(byte_size(0xffffffffffff), 6)
        self.assertEqual(byte_size(0xffffffffffffff), 7)
        self.assertEqual(byte_size(0xffffffffffffffff), 8)
github sybrenstuvel / python-rsa / rsa / pkcs1.py View on Github external
:raise OverflowError: when the message is too large to fit in the padded
        block.

    >>> from rsa import key, common
    >>> (pub_key, priv_key) = key.newkeys(256)
    >>> message = b'hello'
    >>> crypto = encrypt(message, pub_key)

    The crypto text should be just as long as the public key 'n' component:

    >>> len(crypto) == common.byte_size(pub_key.n)
    True

    """

    keylength = common.byte_size(pub_key.n)
    padded = _pad_for_encryption(message, keylength)

    payload = transform.bytes2int(padded)
    encrypted = core.encrypt_int(payload, pub_key.e, pub_key.n)
    block = transform.int2bytes(encrypted, keylength)

    return block
github arangodb / arangodb / 3rdParty / V8 / V8-4.9.391 / tools / swarming_client / third_party / rsa / rsa / pkcs1.py View on Github external
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:])
github taxigps / xbmc-addons-chinese / plugin.video.bdyun / resources / modules / rsa / transform.py View on Github external
"""

    # Type checking
    if not is_integer(number):
        raise TypeError("You must pass an integer for 'number', not %s" %
                        number.__class__)

    if number < 0:
        raise ValueError('Negative numbers cannot be used: %i' % number)

    # Do some bounds checking
    if number == 0:
        needed_bytes = 1
        raw_bytes = [ZERO_BYTE]
    else:
        needed_bytes = common.byte_size(number)
        raw_bytes = []

    # You cannot compare None > 0 in Python 3x. It will fail with a TypeError.
    if block_size and block_size > 0:
        if needed_bytes > block_size:
            raise OverflowError('Needed %i bytes for number, but block size '
                                'is %i' % (needed_bytes, block_size))

    # Convert the number to bytes.
    while number > 0:
        raw_bytes.insert(0, byte(number & 0xFF))
        number >>= 8

    # Pad with zeroes to fill the block
    if block_size and block_size > 0:
        padding = (block_size - needed_bytes) * ZERO_BYTE
github taers232c / GAMADV-XTD3 / src / rsa / pkcs1.py View on Github external
def find_signature_hash(signature, pub_key):
    """Returns the hash name detected from the signature.

    If you also want to verify the message, use :py:func:`rsa.verify()` instead.
    It also returns the name of the used hash.

    :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.
    :returns: the name of the used hash.
    """

    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)

    return _find_method_hash(clearsig)
github CharlesPikachu / DecryptLogin / DecryptLogin / platforms / baidupan.py View on Github external
def encrypt(message, pubkey):
            keylength = rsa.common.byte_size(pubkey.n)
            padded = padMSG(message, keylength)
            payload = rsa.transform.bytes2int(padded)
            encrypted = rsa.core.encrypt_int(payload, pubkey.e, pubkey.n)
            block = rsa.transform.int2bytes(encrypted, keylength)
            return block
        m = int(publickkey_modulus, 16)