diff options
author | Barry Mead <barrymead@cox.net> | 2010-02-17 04:44:59 -0700 |
---|---|---|
committer | Barry Mead <barrymead@cox.net> | 2010-02-17 04:44:59 -0700 |
commit | 86a8862cf3b2719794af3af29560c300ef01cd10 (patch) | |
tree | a039ab44a62e499e1a830e74bab4d9315c6903e3 | |
parent | 1bb49644927ce1c333c2f83e6b498b469c5ae80b (diff) | |
download | rsa-86a8862cf3b2719794af3af29560c300ef01cd10.tar.gz |
Corrected __contains__ and made exceptions come first
-rw-r--r-- | rsa/__init__.py | 24 | ||||
-rw-r--r-- | rsa/fastrsa.py | 30 |
2 files changed, 27 insertions, 27 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py index 68f5953..4e499fc 100644 --- a/rsa/__init__.py +++ b/rsa/__init__.py @@ -496,32 +496,32 @@ def gluechops(string, key, n, funcref): def encrypt(message, key): """Encrypts a string 'message' with the public key 'key'""" - if key.__contains__('n'): - return chopstring(message, key['e'], key['n'], encrypt_int) - else: + if 'n' not in key: raise Exception("You must use the public key with encrypt") + return chopstring(message, key['e'], key['n'], encrypt_int) + def sign(message, key): """Signs a string 'message' with the private key 'key'""" - if key.__contains__('p'): - return chopstring(message, key['d'], key['p']*key['q'], encrypt_int) - else: + if 'p' not in key: raise Exception("You must use the private key with sign") + return chopstring(message, key['d'], key['p']*key['q'], encrypt_int) + def decrypt(cypher, key): """Decrypts a cypher with the private key 'key'""" - if key.__contains__('p'): - return gluechops(cypher, key['d'], key['p']*key['q'], decrypt_int) - else: + if 'p' not in key: raise Exception("You must use the private key with decrypt") + return gluechops(cypher, key['d'], key['p']*key['q'], decrypt_int) + def verify(cypher, key): """Verifies a cypher with the public key 'key'""" - if key.__contains__('n'): - return gluechops(cypher, key['e'], key['n'], decrypt_int) - else: + if 'n' not in key: raise Exception("You must use the public key with verify") + return gluechops(cypher, key['e'], key['n'], decrypt_int) + # Do doctest if we're not imported if __name__ == "__main__": import doctest diff --git a/rsa/fastrsa.py b/rsa/fastrsa.py index 72bb055..8f0617c 100644 --- a/rsa/fastrsa.py +++ b/rsa/fastrsa.py @@ -516,7 +516,7 @@ def chopstring(message, key, funcref): Used by 'encrypt' and 'sign'. """ - if key.__contains__('n'): + if 'n' in key: n = key['n'] #Public key has n already else: n = key['p'] * key['q'] #Private key has p & q @@ -559,32 +559,32 @@ def gluechops(string, key, funcref): def encrypt(message, key): """Encrypts a string 'message' with the public key 'key'""" - if key.__contains__('n'): - return chopstring(message, key, encrypt_int) - else: + if 'n' not in key: raise Exception("You must use the public key with encrypt") + return chopstring(message, key, encrypt_int) + def sign(message, key): """Signs a string 'message' with the private key 'key'""" - if key.__contains__('p'): - return chopstring(message, key, sign_int) - else: + if 'p' not in key: raise Exception("You must use the private key with sign") + return chopstring(message, key, sign_int) + def decrypt(cypher, key): - """Decrypts a cypher with the private key 'key'""" - if key.__contains__('p'): - return gluechops(cypher, key, decrypt_int) - else: + """Decrypts a string 'cypher' with the private key 'key'""" + if 'p' not in key: raise Exception("You must use the private key with decrypt") + return gluechops(cypher, key, decrypt_int) + def verify(cypher, key): - """Verifies a cypher with the public key 'key'""" - if key.__contains__('n'): - return gluechops(cypher, key, verify_int) - else: + """Verifies a string 'cypher' with the public key 'key'""" + if 'n' not in key: raise Exception("You must use the public key with verify") + return gluechops(cypher, key, verify_int) + # Do doctest if we're not imported if __name__ == "__main__": import doctest |