コード例 #1
0
ファイル: _version200.py プロジェクト: AlexisMarie8330/Doll
def to64(number):
    """Converts a number in the range of 0 to 63 into base 64 digit
    character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'.
    
    >>> to64(10)
    'A'
    """

    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError("You must pass a long or an int")

    if 0 <= number <= 9:            #00-09 translates to '0' - '9'
        return byte(number + 48)

    if 10 <= number <= 35:
        return byte(number + 55)     #10-35 translates to 'A' - 'Z'

    if 36 <= number <= 61:
        return byte(number + 61)     #36-61 translates to 'a' - 'z'

    if number == 62:                # 62   translates to '-' (minus)
        return byte(45)

    if number == 63:                # 63   translates to '_' (underscore)
        return byte(95)

    raise ValueError('Invalid Base64 value: %i' % number)
コード例 #2
0
def to64(number):
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    if 0 <= number <= 9:
        return byte(number + 48)
    if 10 <= number <= 35:
        return byte(number + 55)
    if 36 <= number <= 61:
        return byte(number + 61)
    if number == 62:
        return byte(45)
    if number == 63:
        return byte(95)
    raise ValueError('Invalid Base64 value: %i' % number)
コード例 #3
0
ファイル: _version200.py プロジェクト: connoryang/1v1dec
def to64(number):
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    if 0 <= number <= 9:
        return byte(number + 48)
    if 10 <= number <= 35:
        return byte(number + 55)
    if 36 <= number <= 61:
        return byte(number + 61)
    if number == 62:
        return byte(45)
    if number == 63:
        return byte(95)
    raise ValueError('Invalid Base64 value: %i' % number)
コード例 #4
0
def _int2bytes(number, block_size=None):
    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)
    if number == 0:
        needed_bytes = 1
        raw_bytes = [ZERO_BYTE]
    else:
        needed_bytes = common.byte_size(number)
        raw_bytes = []
    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))
    while number > 0:
        raw_bytes.insert(0, byte(number & 255))
        number >>= 8

    if block_size and block_size > 0:
        padding = (block_size - needed_bytes) * ZERO_BYTE
    else:
        padding = EMPTY_BYTE
    return padding + EMPTY_BYTE.join(raw_bytes)
コード例 #5
0
ファイル: transform.py プロジェクト: Jelf10/CouchPotatoServer
def _int2bytes(number, block_size=None):
    r"""Converts a number to a string of bytes.

    Usage::

        >>> _int2bytes(123456789)
        '\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789))
        123456789

        >>> _int2bytes(123456789, 6)
        '\x00\x00\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789, 128))
        123456789

        >>> _int2bytes(123456789, 3)
        Traceback (most recent call last):
        ...
        OverflowError: Needed 4 bytes for number, but block size is 3

    @param number: the number to convert
    @param block_size: the number of bytes to output. If the number encoded to
        bytes is less than this, the block will be zero-padded. When not given,
        the returned block is not padded.

    @throws OverflowError when block_size is given and the number takes up more
        bytes than fit into the block.
    """
    # 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
    else:
        padding = EMPTY_BYTE

    return padding + EMPTY_BYTE.join(raw_bytes)
コード例 #6
0
ファイル: varblock.py プロジェクト: bullseyestudio/guns-game
def write_varint(outfile, value):
    '''Writes a varint to a file.

    @param outfile: the file-like object to write to. It should have a write()
        method.
    @returns the number of written bytes.
    '''

    # there is a big difference between 'write the value 0' (this case) and
    # 'there is nothing left to write' (the false-case of the while loop)

    if value == 0:
        outfile.write('\x00')
        return 1

    written_bytes = 0
    while value > 0:
        to_write = value & 0x7f
        value = value >> 7

        if value > 0:
            to_write |= 0x80

        outfile.write(byte(to_write))
        written_bytes += 1

    return written_bytes
コード例 #7
0
ファイル: varblock.py プロジェクト: bopopescu/proto-quic
def write_varint(outfile, value):
    '''Writes a varint to a file.

    @param outfile: the file-like object to write to. It should have a write()
        method.
    @returns the number of written bytes.
    '''

    # there is a big difference between 'write the value 0' (this case) and
    # 'there is nothing left to write' (the false-case of the while loop)

    if value == 0:
        outfile.write(ZERO_BYTE)
        return 1

    written_bytes = 0
    while value > 0:
        to_write = value & 0x7f
        value = value >> 7

        if value > 0:
            to_write |= 0x80

        outfile.write(byte(to_write))
        written_bytes += 1

    return written_bytes
コード例 #8
0
def read_random_bits(nbits):
    nbytes, rbits = divmod(nbits, 8)
    randomdata = os.urandom(nbytes)
    if rbits > 0:
        randomvalue = ord(os.urandom(1))
        randomvalue >>= 8 - rbits
        randomdata = byte(randomvalue) + randomdata
    return randomdata
コード例 #9
0
def read_random_bits(nbits):
    nbytes, rbits = divmod(nbits, 8)
    randomdata = os.urandom(nbytes)
    if rbits > 0:
        randomvalue = ord(os.urandom(1))
        randomvalue >>= 8 - rbits
        randomdata = byte(randomvalue) + randomdata
    return randomdata
コード例 #10
0
def int2bytes(number):
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    string = ''
    while number > 0:
        string = '%s%s' % (byte(number & 255), string)
        number /= 256

    return string
コード例 #11
0
ファイル: transform.py プロジェクト: Natsukikinb/XMLY
def _int2bytes(number, block_size=None):
    r"""Converts a number to a string of bytes.
    Usage::
        >>> _int2bytes(123456789)
        b'\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789))
        123456789
        >>> _int2bytes(123456789, 6)
        b'\x00\x00\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789, 128))
        123456789
        >>> _int2bytes(123456789, 3)
        Traceback (most recent call last):
        ...
        OverflowError: Needed 4 bytes for number, but block size is 3
    @param number: the number to convert
    @param block_size: the number of bytes to output. If the number encoded to
        bytes is less than this, the block will be zero-padded. When not given,
        the returned block is not padded.
    @throws OverflowError when block_size is given and the number takes up more
        bytes than fit into the block.
    """

    # 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 = [b'\x00']
    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) * b'\x00'
    else:
        padding = b''

    return padding + b''.join(raw_bytes)
コード例 #12
0
ファイル: _version133.py プロジェクト: connoryang/1v1dec
def int2bytes(number):
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    string = ''
    while number > 0:
        string = '%s%s' % (byte(number & 255), string)
        number /= 256

    return string
コード例 #13
0
def encrypt_bigfile(infile, outfile, pub_key):
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #14
0
ファイル: bigfile.py プロジェクト: connoryang/1v1dec
def encrypt_bigfile(infile, outfile, pub_key):
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #15
0
def to64(number):
    """Converts a number in the range of 0 to 63 into base 64 digit
    character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'.
    
    >>> to64(10)
    'A'
    """
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    if 0 <= number <= 9:
        return byte(number + 48)
    if 10 <= number <= 35:
        return byte(number + 55)
    if 36 <= number <= 61:
        return byte(number + 61)
    if number == 62:
        return byte(45)
    if number == 63:
        return byte(95)
    raise ValueError('Invalid Base64 value: %i' % number)
コード例 #16
0
    def test_decoding_failure(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        if is_bytes(a):
            a = ord(a)
        encrypted = encrypted[:5] + byte(a + 1) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
コード例 #17
0
def _int2bytes(number, block_size=None):
    r"""Converts a number to a string of bytes.
    
    Usage::
    
        >>> _int2bytes(123456789)
        '\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789))
        123456789
    
        >>> _int2bytes(123456789, 6)
        '\x00\x00\x07[\xcd\x15'
        >>> bytes2int(_int2bytes(123456789, 128))
        123456789
    
        >>> _int2bytes(123456789, 3)
        Traceback (most recent call last):
        ...
        OverflowError: Needed 4 bytes for number, but block size is 3
    
    @param number: the number to convert
    @param block_size: the number of bytes to output. If the number encoded to
        bytes is less than this, the block will be zero-padded. When not given,
        the returned block is not padded.
    
    @throws OverflowError when block_size is given and the number takes up more
        bytes than fit into the block.
    """
    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)
    if number == 0:
        needed_bytes = 1
        raw_bytes = [ZERO_BYTE]
    else:
        needed_bytes = common.byte_size(number)
        raw_bytes = []
    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))
    while number > 0:
        raw_bytes.insert(0, byte(number & 255))
        number >>= 8

    if block_size and block_size > 0:
        padding = (block_size - needed_bytes) * ZERO_BYTE
    else:
        padding = EMPTY_BYTE
    return padding + EMPTY_BYTE.join(raw_bytes)
コード例 #18
0
ファイル: test_pkcs1.py プロジェクト: tvalentyn/python-rsa
    def test_decoding_failure(self):
        message = struct.pack('>IIII', 0, 0, 0, 1)
        encrypted = pkcs1.encrypt(message, self.pub)

        # Alter the encrypted stream
        a = encrypted[5]
        self.assertIsInstance(a, int)

        altered_a = (a + 1) % 256
        encrypted = encrypted[:5] + byte(altered_a) + encrypted[6:]

        self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted,
                          self.priv)
コード例 #19
0
def int2bytes(number):
    """Converts a number to a string of bytes
    
    >>> bytes2int(int2bytes(123456789))
    123456789
    """
    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError('You must pass a long or an int')
    string = ''
    while number > 0:
        string = '%s%s' % (byte(number & 255), string)
        number /= 256

    return string
コード例 #20
0
def int2bytes(number):
    """Converts a number to a string of bytes
    """

    if not (type(number) is types.LongType or type(number) is types.IntType):
        raise TypeError("You must pass a long or an int")

    string = ""

    while number > 0:
        string = "%s%s" % (byte(number & 0xFF), string)
        number /= 256
    
    return string
コード例 #21
0
def write_varint(outfile, value):
    if value == 0:
        outfile.write(ZERO_BYTE)
        return 1
    written_bytes = 0
    while value > 0:
        to_write = value & 127
        value = value >> 7
        if value > 0:
            to_write |= 128
        outfile.write(byte(to_write))
        written_bytes += 1

    return written_bytes
コード例 #22
0
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.
    
    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with
    
    """
    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)
    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11
    outfile.write(byte(varblock.VARBLOCK_VERSION))
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)
        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #23
0
def int2bytes(number):
    """Converts a number to a string of bytes

    >>> bytes2int(int2bytes(123456789))
    123456789
    """

    if not (type(number) is int or type(number) is int):
        raise TypeError("You must pass a long or an int")

    string = ""

    while number > 0:
        string = "%s%s" % (byte(number & 0xFF), string)
        number /= 256

    return string
コード例 #24
0
def read_random_bits(nbits):
    """Reads 'nbits' random bits.
    If nbits isn't a whole number of bytes, an extra byte will be appended with
    only the lower bits set.
    """

    nbytes, rbits = divmod(nbits, 8)

    # Get the random bytes
    randomdata = os.urandom(nbytes)

    # Add the remaining random bits
    if rbits > 0:
        randomvalue = ord(os.urandom(1))
        randomvalue >>= (8 - rbits)
        randomdata = byte(randomvalue) + randomdata

    return randomdata
コード例 #25
0
ファイル: randnum.py プロジェクト: mlzharov/python-rsa
def read_random_bits(nbits):
    """Reads 'nbits' random bits.

    If nbits isn't a whole number of bytes, an extra byte will be appended with
    only the lower bits set.
    """

    nbytes, rbits = divmod(nbits, 8)

    # Get the random bytes
    randomdata = os.urandom(nbytes)

    # Add the remaining random bits
    if rbits > 0:
        randomvalue = ord(os.urandom(1))
        randomvalue >>= (8 - rbits)
        randomdata = byte(randomvalue) + randomdata

    return randomdata
コード例 #26
0
def write_varint(outfile, value):
    """Writes a varint to a file.
    
    @param outfile: the file-like object to write to. It should have a write()
        method.
    @returns the number of written bytes.
    """
    if value == 0:
        outfile.write(ZERO_BYTE)
        return 1
    written_bytes = 0
    while value > 0:
        to_write = value & 127
        value = value >> 7
        if value > 0:
            to_write |= 128
        outfile.write(byte(to_write))
        written_bytes += 1

    return written_bytes
コード例 #27
0
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    """

    warnings.warn(
        "The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
        "3.4 due to security issues in the VARBLOCK format. See "
        "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
        "for more information.",
        DeprecationWarning,
        stacklevel=2)

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11  # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #28
0
ファイル: bigfile.py プロジェクト: mlzharov/python-rsa
def encrypt_bigfile(infile, outfile, pub_key):
    """Encrypts a file, writing it to 'outfile' in VARBLOCK format.

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    """

    warnings.warn("The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
                  "3.4 due to security issues in the VARBLOCK format. See "
                  "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
                  "for more information.",
                  DeprecationWarning, stacklevel=2)

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11  # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #29
0
ファイル: bigfile.py プロジェクト: 2flcastro/ka-lite
def encrypt_bigfile(infile, outfile, pub_key):
    '''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
    
    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    '''

    if not isinstance(pub_key, key.PublicKey):
        raise TypeError('Public key required, but got %r' % pub_key)

    key_bytes = common.bit_size(pub_key.n) // 8
    blocksize = key_bytes - 11 # keep space for PKCS#1 padding

    # Write the version number to the VARBLOCK file
    outfile.write(byte(varblock.VARBLOCK_VERSION))

    # Encrypt and write each block
    for block in varblock.yield_fixedblocks(infile, blocksize):
        crypto = pkcs1.encrypt(block, pub_key)

        varblock.write_varint(outfile, len(crypto))
        outfile.write(crypto)
コード例 #30
0
 def test_byte(self):
     for i in range(256):
         byt = byte(i)
         self.assertTrue(is_bytes(byt))
         self.assertEqual(ord(byt), i)
コード例 #31
0
 def test_byte(self):
     for i in range(256):
         byt = byte(i)
         self.assertTrue(is_bytes(byt))
         self.assertEqual(ord(byt), i)
コード例 #32
0
ファイル: test_common.py プロジェクト: mgenest/python-rsa
 def test_values(self):
     self.assertEqual(byte(0), b"\x00")
     self.assertEqual(byte(255), b"\xff")
コード例 #33
0
ファイル: test_common.py プロジェクト: Fale/python-rsa
 def test_values(self):
     self.assertEqual(byte(0), b('\x00'))
     self.assertEqual(byte(255), b('\xff'))
コード例 #34
0
ファイル: test_common.py プロジェクト: mlzharov/python-rsa
 def test_values(self):
     self.assertEqual(byte(0), b("\x00"))
     self.assertEqual(byte(255), b("\xff"))
コード例 #35
0
 def test_values(self):
     self.assertEqual(byte(0), b('\x00'))
     self.assertEqual(byte(255), b('\xff'))
コード例 #36
0
ファイル: test_compat.py プロジェクト: yhjflower/python-rsa
 def test_byte(self):
     for i in range(256):
         byt = byte(i)
         self.assertIsInstance(byt, bytes)
         self.assertEqual(ord(byt), i)