diff options
Diffstat (limited to 'rsa/key.py')
-rw-r--r-- | rsa/key.py | 74 |
1 files changed, 37 insertions, 37 deletions
@@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""RSA key generation code. +'''RSA key generation code. Create new keys with the newkeys() function. It will give you a PublicKey and a PrivateKey object. @@ -23,7 +23,7 @@ Loading and saving keys requires the pyasn1 module. This module is imported as late as possible, such that other functionality will remain working in absence of pyasn1. -""" +''' import logging from rsa._compat import b @@ -35,11 +35,11 @@ import rsa.common log = logging.getLogger(__name__) class AbstractKey(object): - """Abstract superclass for private and public keys.""" + '''Abstract superclass for private and public keys.''' @classmethod def load_pkcs1(cls, keyfile, format='PEM'): - r"""Loads a key in PKCS#1 DER or PEM format. + r'''Loads a key in PKCS#1 DER or PEM format. :param keyfile: contents of a DER- or PEM-encoded file that contains the public key. @@ -47,7 +47,7 @@ class AbstractKey(object): :return: a PublicKey object - """ + ''' methods = { 'PEM': cls._load_pkcs1_pem, @@ -63,12 +63,12 @@ class AbstractKey(object): return method(keyfile) def save_pkcs1(self, format='PEM'): - """Saves the public key in PKCS#1 DER or PEM format. + '''Saves the public key in PKCS#1 DER or PEM format. :param format: the format to save; 'PEM' or 'DER' :returns: the DER- or PEM-encoded public key. - """ + ''' methods = { 'PEM': self._save_pkcs1_pem, @@ -84,7 +84,7 @@ class AbstractKey(object): return method() class PublicKey(AbstractKey): - """Represents a public RSA key. + '''Represents a public RSA key. This key is also known as the 'encryption key'. It contains the 'n' and 'e' values. @@ -105,7 +105,7 @@ class PublicKey(AbstractKey): >>> key['e'] 3 - """ + ''' __slots__ = ('n', 'e') @@ -133,7 +133,7 @@ class PublicKey(AbstractKey): @classmethod def _load_pkcs1_der(cls, keyfile): - r"""Loads a key in PKCS#1 DER format. + r'''Loads a key in PKCS#1 DER format. @param keyfile: contents of a DER-encoded file that contains the public key. @@ -150,7 +150,7 @@ class PublicKey(AbstractKey): >>> PublicKey._load_pkcs1_der(der) PublicKey(2367317549, 65537) - """ + ''' from pyasn1.codec.der import decoder (priv, _) = decoder.decode(keyfile) @@ -165,10 +165,10 @@ class PublicKey(AbstractKey): return cls(*as_ints) def _save_pkcs1_der(self): - """Saves the public key in PKCS#1 DER format. + '''Saves the public key in PKCS#1 DER format. @returns: the DER-encoded public key. - """ + ''' from pyasn1.type import univ, namedtype from pyasn1.codec.der import encoder @@ -188,7 +188,7 @@ class PublicKey(AbstractKey): @classmethod def _load_pkcs1_pem(cls, keyfile): - """Loads a PKCS#1 PEM-encoded public key file. + '''Loads a PKCS#1 PEM-encoded public key file. The contents of the file before the "-----BEGIN RSA PUBLIC KEY-----" and after the "-----END RSA PUBLIC KEY-----" lines is ignored. @@ -196,22 +196,22 @@ class PublicKey(AbstractKey): @param keyfile: contents of a PEM-encoded file that contains the public key. @return: a PublicKey object - """ + ''' der = rsa.pem.load_pem(keyfile, 'RSA PUBLIC KEY') return cls._load_pkcs1_der(der) def _save_pkcs1_pem(self): - """Saves a PKCS#1 PEM-encoded public key file. + '''Saves a PKCS#1 PEM-encoded public key file. @return: contents of a PEM-encoded file that contains the public key. - """ + ''' der = self._save_pkcs1_der() return rsa.pem.save_pem(der, 'RSA PUBLIC KEY') class PrivateKey(AbstractKey): - """Represents a private RSA key. + '''Represents a private RSA key. This key is also known as the 'decryption key'. It contains the 'n', 'e', 'd', 'p', 'q' and other values. @@ -242,7 +242,7 @@ class PrivateKey(AbstractKey): >>> pk.coef 8 - """ + ''' __slots__ = ('n', 'e', 'd', 'p', 'q', 'exp1', 'exp2', 'coef') @@ -296,7 +296,7 @@ class PrivateKey(AbstractKey): @classmethod def _load_pkcs1_der(cls, keyfile): - r"""Loads a key in PKCS#1 DER format. + r'''Loads a key in PKCS#1 DER format. @param keyfile: contents of a DER-encoded file that contains the private key. @@ -313,7 +313,7 @@ class PrivateKey(AbstractKey): >>> PrivateKey._load_pkcs1_der(der) PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - """ + ''' from pyasn1.codec.der import decoder (priv, _) = decoder.decode(keyfile) @@ -340,10 +340,10 @@ class PrivateKey(AbstractKey): return cls(*as_ints) def _save_pkcs1_der(self): - """Saves the private key in PKCS#1 DER format. + '''Saves the private key in PKCS#1 DER format. @returns: the DER-encoded private key. - """ + ''' from pyasn1.type import univ, namedtype from pyasn1.codec.der import encoder @@ -377,7 +377,7 @@ class PrivateKey(AbstractKey): @classmethod def _load_pkcs1_pem(cls, keyfile): - """Loads a PKCS#1 PEM-encoded private key file. + '''Loads a PKCS#1 PEM-encoded private key file. The contents of the file before the "-----BEGIN RSA PRIVATE KEY-----" and after the "-----END RSA PRIVATE KEY-----" lines is ignored. @@ -385,22 +385,22 @@ class PrivateKey(AbstractKey): @param keyfile: contents of a PEM-encoded file that contains the private key. @return: a PrivateKey object - """ + ''' der = rsa.pem.load_pem(keyfile, b('RSA PRIVATE KEY')) return cls._load_pkcs1_der(der) def _save_pkcs1_pem(self): - """Saves a PKCS#1 PEM-encoded private key file. + '''Saves a PKCS#1 PEM-encoded private key file. @return: contents of a PEM-encoded file that contains the private key. - """ + ''' der = self._save_pkcs1_der() return rsa.pem.save_pem(der, b('RSA PRIVATE KEY')) def find_p_q(nbits, getprime_func=rsa.prime.getprime, accurate=True): - """'Returns a tuple of two different primes of nbits bits each. + ''''Returns a tuple of two different primes of nbits bits each. The resulting p * q has exacty 2 * nbits bits, and the returned p and q will not be equal. @@ -428,7 +428,7 @@ def find_p_q(nbits, getprime_func=rsa.prime.getprime, accurate=True): >>> common.bit_size(p * q) > 240 True - """ + ''' total_bits = nbits * 2 @@ -445,11 +445,11 @@ def find_p_q(nbits, getprime_func=rsa.prime.getprime, accurate=True): q = getprime_func(qbits) def is_acceptable(p, q): - """Returns True iff p and q are acceptable: + '''Returns True iff p and q are acceptable: - p and q differ - (p * q) has the right nr of bits (when accurate=True) - """ + ''' if p == q: return False @@ -477,10 +477,10 @@ def find_p_q(nbits, getprime_func=rsa.prime.getprime, accurate=True): return (max(p, q), min(p, q)) def calculate_keys(p, q, nbits): - """Calculates an encryption and a decryption key given p and q, and + '''Calculates an encryption and a decryption key given p and q, and returns them as a tuple (e, d) - """ + ''' phi_n = (p - 1) * (q - 1) @@ -500,7 +500,7 @@ def calculate_keys(p, q, nbits): return (e, d) def gen_keys(nbits, getprime_func, accurate=True): - """Generate RSA keys of nbits bits. Returns (p, q, e, d). + '''Generate RSA keys of nbits bits. Returns (p, q, e, d). Note: this can take a long time, depending on the key size. @@ -508,7 +508,7 @@ def gen_keys(nbits, getprime_func, accurate=True): ``q`` will use ``nbits/2`` bits. :param getprime_func: either :py:func:`rsa.prime.getprime` or a function with similar signature. - """ + ''' (p, q) = find_p_q(nbits // 2, getprime_func, accurate) (e, d) = calculate_keys(p, q, nbits // 2) @@ -516,7 +516,7 @@ def gen_keys(nbits, getprime_func, accurate=True): return (p, q, e, d) def newkeys(nbits, accurate=True, poolsize=1): - """Generates public and private keys, and returns them as (pub, priv). + '''Generates public and private keys, and returns them as (pub, priv). The public key is also known as the 'encryption key', and is a :py:class:`rsa.PublicKey` object. The private key is also known as the @@ -535,7 +535,7 @@ def newkeys(nbits, accurate=True, poolsize=1): The ``poolsize`` parameter was added in *Python-RSA 3.1* and requires Python 2.6 or newer. - """ + ''' if nbits < 16: raise ValueError('Key too small') |