diff options
author | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-16 14:30:48 +0530 |
---|---|---|
committer | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-16 14:30:48 +0530 |
commit | 03c51e75de8f9969f3fd5f3885a33ef04ce7348a (patch) | |
tree | 828db6ba67a52aab6323981fbd7d0bfa5074e605 /rsa/key.py | |
parent | f9981d28e8ef4a037f6d2598515e425b6c3fef11 (diff) | |
download | rsa-git-03c51e75de8f9969f3fd5f3885a33ef04ce7348a.tar.gz |
Parellelized testing. Caught a lot of bugs.
Diffstat (limited to 'rsa/key.py')
-rw-r--r-- | rsa/key.py | 62 |
1 files changed, 31 insertions, 31 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 |