Skip to content

Commit 32f256d

Browse files
author
Christian Pfarr
committed
Implement crypto api for strings
1 parent c058c44 commit 32f256d

File tree

7 files changed

+394
-38
lines changed

7 files changed

+394
-38
lines changed

key.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
SOFTWARE.
2424
2525
"""
26+
from io import BytesIO
27+
from sys import byteorder
28+
29+
from utils import trim_empty_utf32bytes
2630

2731

2832
class Key(object):
@@ -92,13 +96,33 @@ def modulo(self):
9296
"""
9397
return self._modulo
9498

95-
def modular_exponentiation(self, data):
99+
def _modular_exponentiation(self, integer):
96100
"""
97-
The operation of modular exponentiation calculates the remainder when an integer data (the base)
98-
raised to the power of the exponent, divided by a positive integer modulo.
101+
The operation of modular exponentiation calculates the remainder when an integer (the base)
102+
raised to the power of the exponent, divided by an modulo.
99103
100-
:param data: Data to process the modular exponentiation.
104+
:param integer: Integer to process the modular exponentiation.
101105
:return: The result of the modular exponentiation.
102106
:rtype: int
103107
"""
104-
return pow(data, self._exponent, self._modulo)
108+
return pow(integer, self._exponent, self._modulo)
109+
110+
def _stream_modular_exponentiation(self, utf32bytes):
111+
"""
112+
Calculates the operation of modular exponentiation over a stream of UTF-32 formatted bytes.
113+
114+
:param utf32bytes: UTF-32 formatted input bytes.
115+
:return: UTF-32 formatted output bytes.
116+
:rtype: bytes
117+
"""
118+
input_stream = BytesIO(utf32bytes)
119+
output_stream = BytesIO()
120+
buffer_length = self.bit_length // 8
121+
buffer = bytearray(buffer_length)
122+
while input_stream.readinto(buffer) > 1:
123+
input_integer = int.from_bytes(buffer, byteorder)
124+
output_integer = self._modular_exponentiation(input_integer)
125+
output_buffer = trim_empty_utf32bytes(output_integer.to_bytes(buffer_length, byteorder))
126+
output_stream.write(output_buffer)
127+
buffer = bytearray(buffer_length)
128+
return output_stream.getvalue()

keypair.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class KeyPair(object):
4141
def __init__(self, bit_length=2048):
4242
"""
4343
Creates the key pair with a given bit length.
44-
Works with 2 seperate processes if possible.
44+
Works with 2 separate processes if possible.
4545
46-
:param bit_length: the lenght of the modulo in bits
46+
:param bit_length: the length of the modulo in bits
4747
"""
4848
worker = Pool(min(2, cpu_count()))
4949
bits = [(bit_length // 2), (bit_length // 2)]

prime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,4 @@ def security(cls):
178178
:return: The security level in percent.
179179
:rtype: float
180180
"""
181-
return (100 * (1 - (1 / (pow(2, cls.SOLOVAY_STRASSEN_ROUNDS)))))
181+
return 100 * (1 - (1 / (pow(2, cls.SOLOVAY_STRASSEN_ROUNDS))))

privatekey.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""
2626

2727
from key import Key
28+
from utils import utf8str2utf32bytes, utf32bytes2utf8str, pack, unpack
2829

2930

3031
class PrivateKey(Key):
@@ -64,22 +65,22 @@ def is_private():
6465
"""
6566
return True
6667

67-
def decipher_int(self, data):
68+
def decipher(self, hex_utf32bytes):
6869
"""
69-
Decipher an integer value.
70+
Deciphers the given hex string of UTF-32 formatted bytes.
7071
71-
:param data: Data to be deciphered.
72-
:return: The deciphered data.
73-
:rtype: int
72+
:param hex_utf32bytes: The enciphered data as hex string of UTF-32 formatted bytes
73+
:return: The deciphered Data as UTF-8 string
74+
:rtype: string
7475
"""
75-
return self.modular_exponentiation(data)
76+
return utf32bytes2utf8str(self._stream_modular_exponentiation(bytes.fromhex(unpack(hex_utf32bytes))))
7677

77-
def sign_int(self, data):
78+
def sign(self, utf8str):
7879
"""
79-
Sign an integer value.
80+
Sign the given plain text as UTF-8 string and return the signed data as a hex representation of UTF-32 bytes.
8081
81-
:param data: Data to be signed.
82-
:return: The signed data
83-
:rtype: int
82+
:param utf8str: The plain text as UTF-8 string.
83+
:return: The signed data as hex representation of UTF-32 bytes.
84+
:rtype: string
8485
"""
85-
return self.modular_exponentiation(data)
86+
return pack(self._stream_modular_exponentiation(utf8str2utf32bytes(utf8str)).hex())

publickey.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""
2626

2727
from key import Key
28+
from utils import utf8str2utf32bytes, utf32bytes2utf8str, pack, unpack
2829

2930

3031
class PublicKey(Key):
@@ -64,23 +65,22 @@ def is_public():
6465
"""
6566
return True
6667

67-
def encipher_int(self, data):
68+
def encipher(self, utf8str):
6869
"""
69-
Encipher an integer value.
70+
Encipher the given UTF-8 String into a hex representation of UTF-32 bytes.
7071
71-
:param data: Data to be enciphered.
72-
:return: The enciphered Data.
73-
:rtype: int
72+
:param utf8str: The input to encipher as UTF-8 string.
73+
:return: Hex representation of UTF-32 bytes.
74+
:rtype: string
7475
"""
75-
return self.modular_exponentiation(data)
76+
return pack(self._stream_modular_exponentiation(utf8str2utf32bytes(utf8str)).hex())
7677

77-
def verify_int(self, data):
78+
def verify(self, hex_utf32bytes):
7879
"""
79-
Verify an integer value.
80-
80+
Verify the given hex representation of UTF-32 bytes and return the plain message as UTF-8 string.
8181
82-
:param data: Data to be verified.
83-
:return: The verified Data.
84-
:rtype: int
82+
:param hex_utf32bytes: Hex representation of UTF-32 string.
83+
:return: The plain message as UTF-8 string.
84+
:rtype: string
8585
"""
86-
return self.modular_exponentiation(data)
86+
return utf32bytes2utf8str(self._stream_modular_exponentiation(bytes.fromhex(unpack(hex_utf32bytes))))

0 commit comments

Comments
 (0)