From 01ea0281aa300dfe4ddca16217d64817e2cd8f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 19 Jul 2011 22:31:53 +0200 Subject: Modeled private and public keys as objects --- rsa/__init__.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'rsa/__init__.py') 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__": -- cgit v1.2.3