-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import secrets | ||
|
||
|
||
def _generate_random_token(size: int) -> bytes: | ||
# TODO: seed | ||
return secrets.token_bytes(size) | ||
|
||
|
||
def split_text(clear_text: str) -> tuple[bytes, bytes]: | ||
clear_text_bytes = bytes(clear_text, "utf-8") | ||
return split1(clear_text_bytes) | ||
|
||
|
||
def split1(source: bytes) -> tuple[bytes, bytes]: | ||
token = _generate_random_token(len(source)) | ||
cipher_text = bytearray() | ||
for i in range(len(source)): | ||
cipher_text.append(source[i] ^ token[i]) | ||
return bytes(token), bytes(cipher_text) | ||
|
||
|
||
def merge1(token1: bytes, token2: bytes) -> bytes: | ||
clear_text = bytearray() | ||
for i in range(len(token1)): | ||
clear_text.append(token1[i] ^ token2[i]) | ||
return bytes(clear_text) | ||
|
||
|
||
def merge_text(token: bytes, cipher_text: bytes) -> str: | ||
return merge1(token, cipher_text).decode("utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from tally_token import split_text, merge_text | ||
|
||
|
||
def test_split_merge(): | ||
"""Test that split_text and merge_text are inverses.""" | ||
clear_text = "Hello World!" | ||
token1, token2 = split_text(clear_text) | ||
assert clear_text == merge_text(token1, token2) |
This file was deleted.
Oops, something went wrong.