Esempio n. 1
0
 def generate(self, bits, randfunc=None, progress_func=None):
     rf = self._get_randfunc(randfunc)
     obj = _RSA.generate_py(
         bits, rf, progress_func)  # TODO: Don't use legacy _RSA module
     key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q,
                                    obj.u)
     return _RSAobj(self, key)
Esempio n. 2
0
 def generate(self, bits, randfunc=None, progress_func=None):
     if bits < 1024 or (bits & 0xff) != 0:
         # pubkey.getStrongPrime doesn't like anything that's not a multiple of 128 and > 512
         raise ValueError("RSA modulus length must be a multiple of 256 and > 1024")
     rf = self._get_randfunc(randfunc)
     obj = _RSA.generate_py(bits, rf, progress_func)    # TODO: Don't use legacy _RSA module
     key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q, obj.u)
     return _RSAobj(self, key)
Esempio n. 3
0
    def generate(self, bits, randfunc=None, progress_func=None, e=65537):
        """Randomly generate a fresh, new RSA key.

        :Parameters:
         bits : int
                            Key length, or size (in bits) of the RSA modulus.
                            It must be a multiple of 256, and no smaller than 1024.

         randfunc : callable
                            Random number generation function; it should accept
                            a single integer N and return a string of random data
                            N bytes long.
                            If not specified, a new one will be instantiated
                            from ``Crypto.Random``.

         progress_func : callable
                            Optional function that will be called with a short string
                            containing the key parameter currently being generated;
                            it's useful for interactive applications where a user is
                            waiting for a key to be generated.

         e : int
                            Public RSA exponent. It must be an odd positive integer.
                            It is typically a small number with very few ones in its
                            binary representation.
                            The default value 65537 (= ``0b10000000000000001`` ) is a safe
                            choice: other common values are 5, 7, 17, and 257.

        :attention: You should always use a cryptographically secure random number generator,
            such as the one defined in the ``Crypto.Random`` module; **don't** just use the
            current time and the ``random`` module.

        :attention: Exponent 3 is also widely used, but it requires very special care when padding
            the message.

        :Return: An RSA key object (`_RSAobj`).

        :Raise ValueError:
            When **bits** is too little or not a multiple of 256, or when
            **e** is not odd or smaller than 2.
        """
        if bits < 1024 or (bits & 0xff) != 0:
            # pubkey.getStrongPrime doesn't like anything that's not a multiple of 256 and >= 1024
            raise ValueError(
                "RSA modulus length must be a multiple of 256 and >= 1024")
        if e % 2 == 0 or e < 3:
            raise ValueError(
                "RSA public exponent must be a positive, odd integer larger than 2."
            )
        rf = self._get_randfunc(randfunc)
        obj = _RSA.generate_py(bits, rf, progress_func,
                               e)  # TODO: Don't use legacy _RSA module
        key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q,
                                       obj.u)
        return _RSAobj(self, key)
Esempio n. 4
0
 def generate(self, bits, randfunc=None, progress_func=None, e=65537):
     if bits < 1024 or bits & 255 != 0:
         raise ValueError(
             'RSA modulus length must be a multiple of 256 and >= 1024')
     if e % 2 == 0 or e < 3:
         raise ValueError(
             'RSA public exponent must be a positive, odd integer larger than 2.'
         )
     rf = self._get_randfunc(randfunc)
     obj = _RSA.generate_py(bits, rf, progress_func, e)
     key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q,
                                    obj.u)
     return _RSAobj(self, key)
Esempio n. 5
0
File: RSA.py Progetto: 5ant/lantern
    def generate(self, bits, randfunc=None, progress_func=None, e=65537):
        """Randomly generate a fresh, new RSA key.

        :Parameters:
         bits : int
                            Key length, or size (in bits) of the RSA modulus.
                            It must be a multiple of 256, and no smaller than 1024.

         randfunc : callable
                            Random number generation function; it should accept
                            a single integer N and return a string of random data
                            N bytes long.
                            If not specified, a new one will be instantiated
                            from ``Crypto.Random``.

         progress_func : callable
                            Optional function that will be called with a short string
                            containing the key parameter currently being generated;
                            it's useful for interactive applications where a user is
                            waiting for a key to be generated.

         e : int
                            Public RSA exponent. It must be an odd positive integer.
                            It is typically a small number with very few ones in its
                            binary representation.
                            The default value 65537 (= ``0b10000000000000001`` ) is a safe
                            choice: other common values are 5, 7, 17, and 257.

        :attention: You should always use a cryptographically secure random number generator,
            such as the one defined in the ``Crypto.Random`` module; **don't** just use the
            current time and the ``random`` module.

        :attention: Exponent 3 is also widely used, but it requires very special care when padding
            the message.

        :Return: An RSA key object (`_RSAobj`).

        :Raise ValueError:
            When **bits** is too little or not a multiple of 256, or when
            **e** is not odd or smaller than 2.
        """
        if bits < 1024 or (bits & 0xff) != 0:
            # pubkey.getStrongPrime doesn't like anything that's not a multiple of 256 and >= 1024
            raise ValueError("RSA modulus length must be a multiple of 256 and >= 1024")
        if e%2==0 or e<3:
            raise ValueError("RSA public exponent must be a positive, odd integer larger than 2.")
        rf = self._get_randfunc(randfunc)
        obj = _RSA.generate_py(bits, rf, progress_func, e)    # TODO: Don't use legacy _RSA module
        key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q, obj.u)
        return _RSAobj(self, key)
Esempio n. 6
0
File: RSA.py Progetto: anandu/pylibs
 def generate(self, bits, randfunc=None, progress_func=None):
     rf = self._get_randfunc(randfunc)
     obj = _RSA.generate_py(bits, rf, progress_func)    # TODO: Don't use legacy _RSA module
     key = self._math.rsa_construct(obj.n, obj.e, obj.d, obj.p, obj.q, obj.u)
     return _RSAobj(self, key)