Skip to content

Commit

Permalink
Implement basis of tally-token
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsuyui committed Jul 15, 2023
1 parent 9674d74 commit 939b131
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ tally-token is a Python library for split data into tokens with same length.
$ pip install tally-token
```

## Example

### split

```python
>>> from tally_token import split_text
>>> split_text("Hello, World!")
(b'qQ\xa5\x97\x84\x88\xd7U%\xfb(k\xa1', b'94\xc9\xfb\xeb\xa4\xf7\x02J\x89D\x0f\x80')
```

### merge

```python
>>> from tally_token import merge_text
>>> merge_text(b'qQ\xa5\x97\x84\x88\xd7U%\xfb(k\xa1', b'94\xc9\xfb\xeb\xa4\xf7\x02J\x89D\x0f\x80')
'Hello, World!'
```

# LICENSE

BSD 3-Clause License
32 changes: 32 additions & 0 deletions tally_token/__init__.py
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")
8 changes: 8 additions & 0 deletions tests/test_basis.py
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)
2 changes: 0 additions & 2 deletions tests/test_something.py

This file was deleted.

0 comments on commit 939b131

Please sign in to comment.