Add the blocks module that encode/decode messages.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
d35fd392d0
commit
0f0be06385
@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
Encoding a message to the 8-bits blocks.
|
||||||
|
Decoding a message from the 8-bits blocks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
|
||||||
|
|
||||||
|
def encode(message: str):
|
||||||
|
"""
|
||||||
|
Encode the UTF-8 string into base64 and converts bytes to a list which element represent a byte in a binary.
|
||||||
|
"""
|
||||||
|
|
||||||
|
encoded = base64.b64encode(message.encode('utf-8'))
|
||||||
|
return map(lambda c: format(c, '08b'), encoded)
|
||||||
|
|
||||||
|
|
||||||
|
def decode(message: list):
|
||||||
|
"""
|
||||||
|
Decode the list of binary string into bytes to decode this with base64 to get the UTF-8 string.
|
||||||
|
"""
|
||||||
|
|
||||||
|
decoded_bytes = bytes(map(lambda c: int(c, 2), message))
|
||||||
|
|
||||||
|
return base64.b64decode(decoded_bytes).decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def encode_bytes(byte_list: list):
|
||||||
|
"""
|
||||||
|
Encode the UTF-8 string into base64 and converts bytes to a list which element represent a byte in a binary.
|
||||||
|
"""
|
||||||
|
|
||||||
|
byte_list = [int(b, 2) for b in byte_list]
|
||||||
|
byte_data = bytes(byte_list)
|
||||||
|
return base64.b64encode(byte_data)
|
||||||
|
|
||||||
|
|
||||||
|
def decode_bytes(message: str):
|
||||||
|
"""
|
||||||
|
Decode the list of binary string into bytes to decode this with base64 to get the UTF-8 string.
|
||||||
|
"""
|
||||||
|
|
||||||
|
decoded = base64.b64decode(message)
|
||||||
|
byte_list = list(decoded)
|
||||||
|
return map(lambda b: format(b, '08b'), byte_list)
|
||||||
|
|
@ -0,0 +1,67 @@
|
|||||||
|
import pytest
|
||||||
|
from cryptography_s_des_exploring.sdes.blocks import encode, decode, encode_bytes, decode_bytes
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("message, expected_message", [
|
||||||
|
# ab -> b'YWI='
|
||||||
|
('ab', ['01011001', '01010111', '01001001', '00111101']),
|
||||||
|
|
||||||
|
# bc -> b'YmM='
|
||||||
|
('bc', ['01011001', '01101101', '01001101', '00111101']),
|
||||||
|
|
||||||
|
# абвґд -> b'0LDQsdCy0pHQtA=='
|
||||||
|
('абвґд', ['00110000', '01001100', '01000100', '01010001', '01110011',
|
||||||
|
'01100100', '01000011', '01111001', '00110000', '01110000',
|
||||||
|
'01001000', '01010001', '01110100', '01000001', '00111101',
|
||||||
|
'00111101']),
|
||||||
|
|
||||||
|
# їґєі -> b'0ZfSkdGU0ZY='
|
||||||
|
('їґєі', ['00110000', '01011010', '01100110', '01010011', '01101011',
|
||||||
|
'01100100', '01000111', '01010101', '00110000', '01011010',
|
||||||
|
'01011001', '00111101']),
|
||||||
|
])
|
||||||
|
def test_encode(message, expected_message):
|
||||||
|
result = list(encode(message))
|
||||||
|
assert expected_message == result
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("expected_message, encoded_message", [
|
||||||
|
# ab -> b'YWI='
|
||||||
|
('ab', ['01011001', '01010111', '01001001', '00111101']),
|
||||||
|
|
||||||
|
# bc -> b'YmM='
|
||||||
|
('bc', ['01011001', '01101101', '01001101', '00111101']),
|
||||||
|
|
||||||
|
# абвґд -> b'0LDQsdCy0pHQtA=='
|
||||||
|
('абвґд', ['00110000', '01001100', '01000100', '01010001', '01110011',
|
||||||
|
'01100100', '01000011', '01111001', '00110000', '01110000',
|
||||||
|
'01001000', '01010001', '01110100', '01000001', '00111101',
|
||||||
|
'00111101']),
|
||||||
|
|
||||||
|
# їґєі -> b'0ZfSkdGU0ZY='
|
||||||
|
('їґєі', ['00110000', '01011010', '01100110', '01010011', '01101011',
|
||||||
|
'01100100', '01000111', '01010101', '00110000', '01011010',
|
||||||
|
'01011001', '00111101']),
|
||||||
|
])
|
||||||
|
def test_decode(expected_message, encoded_message):
|
||||||
|
result = decode(encoded_message)
|
||||||
|
assert expected_message == result
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("bytes, expected_message", [
|
||||||
|
(['01011001', '01010111', '01001001', '00111101'], b'WVdJPQ=='),
|
||||||
|
(['01011001', '01010111', '00000000', '00111101'], b'WVcAPQ==')
|
||||||
|
])
|
||||||
|
def test_encode_bytes(bytes, expected_message):
|
||||||
|
result = encode_bytes(bytes)
|
||||||
|
assert expected_message == result
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("expected_bytes, message", [
|
||||||
|
(['01011001', '01010111', '01001001', '00111101'], 'WVdJPQ=='),
|
||||||
|
(['01011001', '01010111', '00000000', '00111101'], 'WVcAPQ==')
|
||||||
|
])
|
||||||
|
def test_encode_bytes(expected_bytes, message):
|
||||||
|
result = list(decode_bytes(message))
|
||||||
|
assert expected_bytes == result
|
||||||
|
|
Loading…
Reference in new issue