forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero_width.py
More file actions
24 lines (16 loc) · 954 Bytes
/
Copy pathzero_width.py
File metadata and controls
24 lines (16 loc) · 954 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
# -*- coding: UTF-8 -*-
"""Zero-width Unicode binary steganography codec."""
from ..__common__ import *
ZERO, ONE, SEPARATOR = "\u200b", "\u200c", "\u200d"
def zero_width_encode(text, errors="strict"):
result = SEPARATOR.join("".join(ONE if bit == "1" else ZERO for bit in format(byte, "08b")) for byte in b(text))
return result, len(result)
def zero_width_decode(text, errors="strict"):
text = ensure_str(text)
bits = "".join("0" if char == ZERO else "1" for char in text if char in (ZERO, ONE))
if len(bits) % 8 and errors == "strict":
raise ZeroWidthDecodeError("zero-width bit count must be divisible by eight")
result = bytes(int(bits[index:index + 8], 2) for index in range(0, len(bits) // 8 * 8, 8))
return result, len(text)
add("zero_width", zero_width_encode, zero_width_decode, r"^zero[-_]?width(?:[-_]?steg(?:o)?)?$",
aliases=["zero-width"], entropy=1., printables_rate=0., expansion_factor=8.)