Skip to content

Commit

Permalink
add xor_encode_decode for simple enc
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Jun 11, 2024
1 parent c4346d9 commit e387832
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Module Docs - https://github.com/ClericPy/morebuiltins/blob/master/doc.md

1.19 `GuessExt` - Determines whether the input bytes of a file prefix indicate a compressed file format.

1.20 `xor_encode_decode` -


## 2. morebuiltins.functools

Expand Down Expand Up @@ -115,6 +117,8 @@ Module Docs - https://github.com/ClericPy/morebuiltins/blob/master/doc.md

## 5. morebuiltins.download_python

5.1 `download_python` - Download python portable interpreter from https://github.com/indygreg/python-build-standalone/releases. `python -m morebuiltins.download_python`


<!-- end -->

Expand Down
83 changes: 82 additions & 1 deletion doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,39 @@
1.19 `GuessExt` - Determines whether the input bytes of a file prefix indicate a compressed file format.

>>> cg = GuessExt()
>>> cg.get_ext(b"PKzipfiledemo")
>>> cg.get_ext(b"PK\x05\x06zipfiledemo")
'.zip'


---


1.20 `xor_encode_decode` -
Perform XOR encryption or decryption on the given data using a provided key.

This function encrypts or decrypts the data by performing an XOR operation
between each byte of the data and the corresponding byte of the key. The key
is repeated if necessary to cover the entire length of the data.

Parameters:
- data: The byte data to be encrypted or decrypted.
- key: The key used for encryption or decryption, also a sequence of bytes.

Returns:
- The resulting byte data after encryption or decryption.

Example:

>>> original_data = b'Hello, World!'
>>> key = b'secret'
>>> encrypted_data = xor_encode_decode(original_data, key)
>>> encrypted_data
b';\x00\x0f\x1e\nXS2\x0c\x00\t\x10R'
>>> decrypted_data = xor_encode_decode(encrypted_data, key)
>>> decrypted_data == original_data
True


---

======================
Expand Down Expand Up @@ -793,3 +822,55 @@

======================


5.1 `download_python` - Download python portable interpreter from https://github.com/indygreg/python-build-standalone/releases. `python -m morebuiltins.download_python`

λ python -m morebuiltins.download_python
[10:56:17] Checking https://api.github.com/repos/indygreg/python-build-standalone/releases/latest
[10:56:19] View the rules:
https://gregoryszorc.com/docs/python-build-standalone/main/running.html#obtaining-distributions

[10:56:19] Got 290 urls from github.

[290] Enter keywords (can be int index or partial match, defaults to 0):
0. windows
1. linux
2. darwin
0
[10:56:24] Filt with keyword: "windows". 290 => 40

[40] Enter keywords (can be int index or partial match, defaults to 0):
0. 3.12.3
1. 3.11.9
2. 3.10.14
3. 3.9.19
4. 3.8.19

[10:56:25] Filt with keyword: "3.12.3". 40 => 8

[8] Enter keywords (can be int index or partial match, defaults to 0):
0. x86_64
1. i686

[10:56:28] Filt with keyword: "x86_64". 8 => 4

[4] Enter keywords (can be int index or partial match, defaults to 0):
0. shared-pgo-full.tar.zst
1. shared-install_only.tar.gz
2. pgo-full.tar.zst
3. install_only.tar.gz
3
[10:56:33] Filt with keyword: "install_only.tar.gz". 4 => 1
[10:56:33] Download URL: 39.1 MB
https://github.com/indygreg/python-build-standalone/releases/download/20240415/cpython-3.12.3%2B20240415-x86_64-pc-windows-msvc-install_only.tar.gz
File path to save(defaults to `./cpython-3.12.3+20240415-x86_64-pc-windows-msvc-install_only.tar.gz`)?
or `q` to exit.

[10:56:38] Start downloading...
https://github.com/indygreg/python-build-standalone/releases/download/20240415/cpython-3.12.3%2B20240415-x86_64-pc-windows-msvc-install_only.tar.gz
D:\github\morebuiltins\morebuiltins\download_python\cpython-3.12.3+20240415-x86_64-pc-windows-msvc-install_only.tar.gz
[10:56:44] Downloading: 39.12 / 39.12 MB | 100.00% | 11.3 MB/s | 0s
[10:56:44] Download complete.

---

2 changes: 1 addition & 1 deletion morebuiltins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.5"
__version__ = "0.0.6"
__all__ = [
"morebuiltins.utils",
"morebuiltins.functools",
Expand Down
6 changes: 5 additions & 1 deletion morebuiltins/download_python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from .zipapps.download_python import download_python

__all__ = ["download_python"]


if __name__ == "__main__":
from .zipapps.download_python import download_python
download_python()
36 changes: 35 additions & 1 deletion morebuiltins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"format_error",
"Trie",
"GuessExt",
"xor_encode_decode",
]


Expand Down Expand Up @@ -757,7 +758,7 @@ def match(self, seq, default: Tuple[Union[None, int], Any] = (None, None)):


class GuessExt(object):
"""Determines whether the input bytes of a file prefix indicate a compressed file format.
r"""Determines whether the input bytes of a file prefix indicate a compressed file format.
>>> cg = GuessExt()
>>> cg.get_ext(b"PK\x05\x06zipfiledemo")
Expand Down Expand Up @@ -803,6 +804,39 @@ def get_ext(self, magic_bytes: bytes, default=""):
return value


def xor_encode_decode(data, key):
r"""
Perform XOR encryption or decryption on the given data using a provided key.
This function encrypts or decrypts the data by performing an XOR operation
between each byte of the data and the corresponding byte of the key. The key
is repeated if necessary to cover the entire length of the data.
Parameters:
- data: The byte data to be encrypted or decrypted.
- key: The key used for encryption or decryption, also a sequence of bytes.
Returns:
- The resulting byte data after encryption or decryption.
Example:
>>> original_data = b'Hello, World!'
>>> key = b'secret'
>>> encrypted_data = xor_encode_decode(original_data, key)
>>> encrypted_data
b';\x00\x0f\x1e\nXS2\x0c\x00\t\x10R'
>>> decrypted_data = xor_encode_decode(encrypted_data, key)
>>> decrypted_data == original_data
True
"""
# Extend the key to ensure its length is at least as long as the data
extended_key = key * (len(data) // len(key) + 1)
# Perform XOR operation between each byte of the data and the extended key,
# and return the new sequence of bytes
return bytes([b ^ k for b, k in zip(data, extended_key)])


if __name__ == "__main__":
__name__ = "morebuiltins.utils"
import doctest
Expand Down

0 comments on commit e387832

Please sign in to comment.