diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-07-19 22:31:53 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2011-07-19 22:31:53 +0200 |
commit | 01ea0281aa300dfe4ddca16217d64817e2cd8f60 (patch) | |
tree | 3d55b8c8ffdb700b222d53af840c1a4d7926efc5 /rsa/__init__.py | |
parent | 2404a5e5d04079ec591f8c2eed1e3b5349a70870 (diff) | |
download | rsa-01ea0281aa300dfe4ddca16217d64817e2cd8f60.tar.gz |
Modeled private and public keys as objects
Diffstat (limited to 'rsa/__init__.py')
-rw-r--r-- | rsa/__init__.py | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py index 063889c..006dd7c 100644 --- a/rsa/__init__.py +++ b/rsa/__init__.py @@ -113,31 +113,35 @@ def gluechops(string, key, n, funcref): def encrypt(message, key): """Encrypts a string 'message' with the public key 'key'""" - if 'n' not in key: - raise Exception("You must use the public key with encrypt") - return chopstring(message, key['e'], key['n'], encrypt_int) + if not isinstance(key, keygen.PublicKey): + raise TypeError("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 '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) + if not isinstance(key, keygen.PrivateKey): + raise TypeError("You must use the private key with sign") + + return chopstring(message, key.d, key.n, encrypt_int) def decrypt(cypher, key): """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['d'], key['p']*key['q'], decrypt_int) + if not isinstance(key, keygen.PrivateKey): + raise TypeError("You must use the private key with decrypt") + + return gluechops(cypher, key.d, key.n, decrypt_int) def verify(cypher, key): """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['e'], key['n'], decrypt_int) + if not isinstance(key, keygen.PublicKey): + raise TypeError("You must use the public key with verify") + + return gluechops(cypher, key.e, key.n, decrypt_int) # Do doctest if we're run directly if __name__ == "__main__": |