summaryrefslogtreecommitdiff
path: root/rsa/core.py
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2011-07-30 19:53:11 +0200
committerSybren A. Stüvel <sybren@stuvel.eu>2011-07-30 19:53:11 +0200
commit877fce05e5c26c1c3339b06918a83cb19797ff4b (patch)
treeaec4adeaf4fcead2a1db56e0f56c0c6038318583 /rsa/core.py
parenta3c476ed5fc469f41d820d7db6eedaa61575f184 (diff)
downloadrsa-git-877fce05e5c26c1c3339b06918a83cb19797ff4b.tar.gz
Better type checking in core, casting ASN-ints to Python int
Diffstat (limited to 'rsa/core.py')
-rw-r--r--rsa/core.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/rsa/core.py b/rsa/core.py
index b66eaea..cc95f59 100644
--- a/rsa/core.py
+++ b/rsa/core.py
@@ -21,14 +21,19 @@ mathematically on integers.
import types
+def assert_int(var, name):
+
+ if type(var) in (types.IntType, types.LongType):
+ return
+
+ raise TypeError('%s should be an integer, not %s' % (name, var.__class__))
+
def encrypt_int(message, ekey, n):
"""Encrypts a message using encryption key 'ekey', working modulo n"""
- if type(message) is types.IntType:
- message = long(message)
-
- if not type(message) is types.LongType:
- raise TypeError("You must pass a long or int")
+ assert_int(message, 'message')
+ assert_int(ekey, 'ekey')
+ assert_int(n, 'n')
if message < 0:
raise ValueError('Only non-negative numbers are supported')
@@ -42,6 +47,14 @@ def decrypt_int(cyphertext, dkey, n):
"""Decrypts a cypher text using the decryption key 'dkey', working
modulo n"""
+ if type(cyphertext) not in (types.IntType, types.LongType):
+ raise TypeError('cyphertext should be an integer, not %s' %
+ cyphertext.__type__)
+
+ assert_int(cyphertext, 'cyphertext')
+ assert_int(dkey, 'dkey')
+ assert_int(n, 'n')
+
message = pow(cyphertext, dkey, n)
return message