Skip to content

Commit c058c44

Browse files
committed
implement properties
1 parent 0f90478 commit c058c44

File tree

6 files changed

+70
-44
lines changed

6 files changed

+70
-44
lines changed

key.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,16 @@ def __init__(self, modulo, exponent):
4040
self._modulo = modulo
4141
self._exponent = exponent
4242

43+
@property
4344
def bit_length(self):
4445
"""
4546
Returns the length of the key in bit
4647
47-
:return: length of the key in bit
48+
:return: Length of the key in bit
4849
:rtype: int
4950
"""
5051
return self._modulo.bit_length()
5152

52-
def _get_modulo(self):
53-
"""
54-
Returns the modulo of the key
55-
56-
:return: modulo of the key
57-
:rtype: int
58-
"""
59-
return self._modulo
60-
61-
def _get_exponent(self):
62-
"""
63-
Returns the exponent of the key
64-
65-
:return: exponent of the key
66-
:rtype: int
67-
"""
68-
return self._exponent
69-
7053
@staticmethod
7154
def is_public():
7255
"""
@@ -89,8 +72,25 @@ def is_private():
8972
"""
9073
return False
9174

92-
exponent = property(_get_exponent)
93-
modulo = property(_get_modulo)
75+
@property
76+
def exponent(self):
77+
"""
78+
Get the exponent of the key.
79+
80+
:return: Exponent of the key.
81+
:rtype: int
82+
"""
83+
return self._exponent
84+
85+
@property
86+
def modulo(self):
87+
"""
88+
Get the modulo of the key.
89+
90+
:return: Modulo of the key.
91+
:rtype: int
92+
"""
93+
return self._modulo
9494

9595
def modular_exponentiation(self, data):
9696
"""

keypair.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ def __init__(self, bit_length=2048):
6666
self._public_key = PublicKey(modulo, encipher_exponent)
6767

6868
def __del__(self):
69-
pass
69+
"""
70+
Deletes the key pair.
71+
"""
72+
del self._private_key
73+
del self._public_key
7074

71-
def _get_private_key(self):
75+
@property
76+
def private_key(self):
77+
"""
78+
Returns the private key of the key pair.
79+
80+
:return: Private key.
81+
:rtype: PrivateKey
82+
"""
7283
return self._private_key
7384

74-
def _get_public_key(self):
75-
return self._public_key
85+
@property
86+
def public_key(self):
87+
"""
88+
Returns the public key of the key pair.
7689
77-
private_key = property(_get_private_key)
78-
public_key = property(_get_public_key)
90+
:return: Public key.
91+
:rtype: PublicKey
92+
"""
93+
return self._public_key

prime.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,16 @@ def __truediv__(self, other):
143143
else:
144144
return self._value / other
145145

146-
def _get_value(self):
146+
@property
147+
def value(self):
147148
"""
148-
Returns the value of the prime number
149+
Returns the value of the prime number.
149150
150-
:return: The value
151+
:return: The value.
151152
:rtype: int
152153
"""
153154
return self._value
154155

155-
value = property(_get_value)
156-
157156
def _is_primality(self, maybe):
158157
"""
159158
Checks if the potential prime number is a prime number via Solovay Strassen Test
@@ -172,4 +171,11 @@ def _is_primality(self, maybe):
172171

173172
@classmethod
174173
def security(cls):
174+
"""
175+
Calculates the security level in percent.
176+
To change the security level, just change the static value SOLOVAY_STRASSEN_ROUNDS.
177+
178+
:return: The security level in percent.
179+
:rtype: float
180+
"""
175181
return (100 * (1 - (1 / (pow(2, cls.SOLOVAY_STRASSEN_ROUNDS)))))

privatekey.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ def __init__(self, modulo, decipher_exponent):
4242
"""
4343
Key.__init__(self, modulo, decipher_exponent)
4444

45-
def _get_decipher_exponent(self):
45+
@property
46+
def decipher_exponent(self):
4647
"""
4748
Returns the decipher exponent of the private key.
4849
4950
:return: decipher exponent of the private key
5051
:rtype: int
5152
"""
52-
return self._get_exponent()
53+
return self._exponent
5354

5455
@staticmethod
5556
def is_private():
@@ -63,8 +64,6 @@ def is_private():
6364
"""
6465
return True
6566

66-
decipher_exponent = property(_get_decipher_exponent)
67-
6867
def decipher_int(self, data):
6968
"""
7069
Decipher an integer value.

publickey.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ def __init__(self, modulo, encipher_exponent):
4242
"""
4343
Key.__init__(self, modulo, encipher_exponent)
4444

45-
def _get_encipher_exponent(self):
45+
@property
46+
def encipher_exponent(self):
4647
"""
4748
Returns the encipher exponent of the public key.
4849
4950
:return: the encipher exponent of the public key
5051
:rtype: int
5152
"""
52-
return self._get_exponent()
53+
return self._exponent
5354

5455
@staticmethod
5556
def is_public():
@@ -63,8 +64,6 @@ def is_public():
6364
"""
6465
return True
6566

66-
encipher_exponent = property(_get_encipher_exponent)
67-
6867
def encipher_int(self, data):
6968
"""
7069
Encipher an integer value.

test/keypair_testcase.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ def setUp(self):
3535
def tearDown(self):
3636
del self.bits
3737

38-
def test_key_pair(self):
39-
key_pair = KeyPair(self.bits)
40-
self.assertTrue(key_pair.public_key.modulo.bit_length() == self.bits,
41-
"Key pair constructor creates wrong bitlength modulo")
38+
def test_key_pair_bits(self):
39+
self.assertTrue(KeyPair(self.bits).public_key.modulo.bit_length() == self.bits,
40+
"Key pair constructor creates wrong bit length modulo")
41+
42+
def test_public_key_bits(self):
43+
self.assertTrue(KeyPair(self.bits).public_key.bit_length == self.bits,
44+
"Key pair constructor creates wrong bit length public key")
45+
46+
def test_private_key_bits(self):
47+
self.assertTrue(KeyPair(self.bits).private_key.bit_length == self.bits,
48+
"Key pair constructor creates wrong bit length private key")

0 commit comments

Comments
 (0)