forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor.py
More file actions
executable file
·35 lines (25 loc) · 991 Bytes
/
Copy pathxor.py
File metadata and controls
executable file
·35 lines (25 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: UTF-8 -*-
"""XOR Codec - XOR-with-1-byte content encoding.
This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""
from ..__common__ import *
__examples__ = {
'enc(xor0|xor--10|xor256|xor300)': None,
'enc(xor3|xor-3|xor_3)': {'this is a test': "wkjp#jp#b#wfpw"},
'enc(xor3|xor-3|xor_3)': {'wkjp#jp#b#wfpw': "this is a test"},
'enc(xor6|xor-6|xor_6)': {'this is a test': "rnou&ou&g&rcur"},
}
__guess__ = ["xor-%d" % i for i in range(1, 256)]
def _xorn(text, n=1):
return "".join(chr(ord(c) ^ (n % 256)) for c in text)
def xor_byte_encode(i):
def encode(text, errors="strict"):
r = _xorn(ensure_str(text), i)
return r, len(r)
return encode
add("xor", xor_byte_encode, xor_byte_encode, r"^xor[-_]?([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$",
transitive=True)