forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift.py
More file actions
34 lines (24 loc) · 967 Bytes
/
Copy pathshift.py
File metadata and controls
34 lines (24 loc) · 967 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 -*-
"""Shift Codec - Shift-ordinal-with-N 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(shift0|shift--10|shift256)': None,
'enc(shift1|shift_1|shift-1)': {'this is a test': "uijt!jt!b!uftu"},
'enc(shift9|shift_9|shift-9)': {'this is a test': "}qr|)r|)j)}n|}"},
}
__guess__ = ["shift-%d" % i for i in range(1, 256)]
def ord_shift_decode(i):
return ord_shift_encode(-int(i))
def ord_shift_encode(i):
def encode(text, errors="strict"):
r = "".join(chr((ord(c) + int(i)) % 256) for c in text)
return r, len(r)
return encode
add("shift", ord_shift_encode, ord_shift_decode, r"shift[-_]?([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$",
transitive=True)