Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implementation of "https?://some.domain.under.cdn.cloudflare.example.com/cdn-cgi/l/email-protection#{encoded_email_address}".
def protect_email(key: int, data: bytes) -> bytes:
return bytes([x ^ (key % 256) for x in b"\x00" + data])
def deprotect_email(data: bytes) -> bytes:
return bytes([x ^ data[0] for x in data[1:]])
def main():
import argparse
import random
def str_to_bytes(data: str) -> bytes:
return bytes([int(x + y, base=16) for x, y in zip(*[iter(data)] * 2)])
def bytes_to_str(data: bytes) -> str:
return "".join(["{:02x}".format(x) for x in data])
parser = argparse.ArgumentParser(description="Encode or decode your e-mail address.")
parser.add_argument("data", type=str, nargs="?", default="",
help="a encoded or plain e-mail address")
parser.add_argument("-d", "--decode", dest="act", action="store_const",
const=lambda _, x: str(deprotect_email(str_to_bytes(x)), "utf-8"),
default=lambda k, b: bytes_to_str(protect_email(k, bytes(b, "utf-8"))),
help="decode an encoded e-mail address")
parser.add_argument("-k", "--key", type=int, default=random.randint(0, 255),
help="key to encode 'data' (ignore in decode mode)")
args = parser.parse_args()
return args.act(args.key, args.data if args.data else input())
if __name__ == "__main__":
print(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment